Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.cloneElement: pass new children or copy props.children?

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?

like image 379
Fabian Zeindl Avatar asked May 09 '16 16:05

Fabian Zeindl


People also ask

How do you pass props with children in React?

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.

Can you pass props from child to child?

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.

What is React cloneElement and the difference with this props children?

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.

Should you use React cloneElement?

React. cloneElement() is useful when you want to add or modify the props of a parent component's children while avoiding unnecessary duplicate code.


1 Answers

Should I put the mapped children into my newProps.children or should I pass them as the third parameter to cloneElement?

Either should be fine.

Object.assign copied the children from props to newProps 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.

like image 87
Sophie Alpert Avatar answered Nov 10 '22 02:11

Sophie Alpert