I'm confused by the third "children" parameter of React.cloneElement
and it's relation to this.props.children
.
I followed this guide on higher order components and have the following code:
render() {
const elementsTree = super.render()
let myPropChange = {}
/* work on newProps... */
myPropChange.something = "nice!".
const newProps = Object.assign({}, elementsTree.props, myPropChange)
/* map children */
const newChildren = React.Children.map(elementsTree.props.children, c => something(c))
return React.cloneElement(elementsTree, newProps, newChildren)
}
Should I put the mapped children into my newProps.children
or should I pass them as the third parameter to cloneElement
?
Object.assign
copied the children from props
to newProps
anyway, should I skip them?
In the guide it says
Components don’t have a guaranty of having the full children tree resolved.
What does that mean in my situation? That this.props.children
is not there?
Added 4th question: Why should I clone the props at all and not just directly edit them?
Passing Props to Children in React Using the Context API You can pass it down manually, but it'll be time-consuming and hard to track. Instead, you can use Context to make the value available in every child component. Technically, Context is not the same as props , but it gets the job done in our situation.
There is no way to pass props up to a parent component from a child component. We will revisit this caveat later in this tutorial. It's also important to note that React's props are read only (immutable). As a developer, you should never mutate props but only read them in your components.
The React. cloneElement() function returns a copy of a specified element. Additional props and children can be passed on in the function. You would use this function when a parent component wants to add or modify the prop(s) of its children.
React. cloneElement() is useful when you want to add or modify the props of a parent component's children while avoiding unnecessary duplicate code.
Should I put the mapped children into my
newProps.children
or should I pass them as the third parameter tocloneElement
?
Either should be fine.
Object.assign
copied the children fromprops
tonewProps
anyway, should I skip them?
When using cloneElement, you don't need to copy the props yourself. You can just do React.cloneElement(elementsTree, {something: 'nice!'})
.
In the guide it says "Components don’t have a guaranty of having the full children tree resolved." What does that mean in my situation? That this.props.children is not there?
I can't be sure what that article meant, but I believe the author means that your component can choose not to use this.props.children
. For example, if you render <Foo><Bar /></Foo>
, then Foo
will receive <Bar />
in this.props.children
, but if Foo
doesn't use this.props.children
in its render method, then Bar
will never be rendered.
Why should I clone the props at all and not just directly edit them?
React elements are immutable and there's no way you can change the props after an element is created. This allows some performance optimizations in React which wouldn't be possible otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With