Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  28] [ 3]  / answers: 1 / hits: 22452  / 6 Years ago, thu, march 15, 2018, 12:00:00

In React v16.2.0, there is a new API call React.Children.



I am curious whats the different between React.Children and use children directly.



For example, if I want to manipulate the children content, I can do the trick in both methods. example



const Child = () => (
<div>child</div>
)

class App extends React.Component {
render() {
const template1 = React.Children.map(this.props.children, (child) => {
return React.cloneElement(child);
});

const template2 = this.props.children.map((child) => {
return React.cloneElement(child);
});
return [template1, template2];
}
}


And the result is the same.



Does anyone know what is the different?



Or what is the purpose for react team to release this API.



Thank you.


More From » reactjs

 Answers
45

To the best of my knowledge, for your operations there is no real difference. However, React.Children provides utilities for dealing with this.props.children. For the use of map there was one comment in the documentation that I thought might be interesting:




If children is a keyed fragment or array it will be traversed




So their implementation seems to be doing a little more than if your just used Array.prototype.map.



You can read on what other functionality React.Children provides, but it largely seems like they are providing convenience functions so you don't have to worry about traversing children who may have children and so on.


[#54936] Monday, March 12, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alysas

Total Points: 616
Total Questions: 111
Total Answers: 124

Location: Slovenia
Member since Wed, Apr 6, 2022
2 Years ago
;