Skip to content

Commit a5cc7dc

Browse files
committed
Rendering Adjacent JSX Elements using JS Arrays
1 parent 0b96649 commit a5cc7dc

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ In this repository, I've implemented (or implementing) all the concepts related
8787
2. Using `React.memo()` for optimizing functional components: [Commit Details](https://github.com/Ch-sriram/react/commit/0b5aecceb6914057e212a9901131b54c180e63b5)
8888
3. Checking all the `props` info using `shouldComponentUpdate`: [Commit Details](https://github.com/Ch-sriram/react/commit/6dec77afa3170c223fc833adf04bd72ed34d3006)
8989
4. Delegating the task done in 8.3 to a `PureComponent` by extending a `PureComponent`: [Commit Details](https://github.com/Ch-sriram/react/commit/8efc19ec3559c40a96ac32e13588c3aa04f2e4ae)
90+
9. Rendering Adjacent JSX Elements
91+
1. Using JS Arrays/Lists: [Commit Details]()

react-examples/src/components/Persons/Person/Person.component.jsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ import PersonStyleClasses from './Person-style.module.css';
44
class Person extends Component {
55
render() {
66
console.log("[Person.jsx] rendering...");
7-
return (
8-
<div className={PersonStyleClasses.Person}>
9-
<p onClick={this.props.click}>
10-
I'm {this.props.name} and I'm {this.props.age} years old!
11-
</p>
12-
<input
13-
type="text"
14-
onChange={this.props.changed}
15-
value={this.props.name}
16-
/>
17-
</div>
18-
);
7+
/**
8+
* What we have to take care when we return a list of
9+
* components as JSX, in that case, we have to give a `key`
10+
* property to every component inside the list.
11+
*/
12+
return [
13+
<p key="comp1" onClick={this.props.click}>
14+
I'm {this.props.name} and I'm {this.props.age} years old!
15+
</p>,
16+
<p key={"comp2"}>{this.props.children}</p>,
17+
<input
18+
key="comp3"
19+
type="text"
20+
onChange={this.props.changed}
21+
value={this.props.name}
22+
/>
23+
];
1924
}
2025
}
2126

react-examples/src/components/Persons/Persons.component.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ class Persons extends PureComponent {
4545

4646
render() {
4747
console.log("[Persons.jsx] rendering...");
48+
/**
49+
* Here, we are returning a list of <Person/> components,
50+
* not components which are wrapped inside a <div>, and so
51+
* in React, this is absolutely possible.
52+
*
53+
* Therefore, from any kind of a component, we can actually
54+
* just return a single <div> or a list of components as we
55+
* have done below.
56+
*
57+
* This same approach will be used to render the components
58+
* of the <Person/> component.
59+
*/
4860
return this.props.persons.map((person, index) => {
4961
return (
5062
<Person

0 commit comments

Comments
 (0)