K
K
KnightForce2017-05-09 22:37:48
React
KnightForce, 2017-05-09 22:37:48

Reason for duplicating element from props.children on React.cloneElement(child)?

Such a design:
1) Option.

renderChildren(props) {

    let child = Children.only(props.children);
    return cloneElement(child, Object.assign({}, props));
  }

  render() {
    return (
      this.renderChildren(this.props)
    );
  }

Option 2:
renderChildren(props) {

    let child = Children.only(props.children);
    return cloneElement(child, Object.assign({}, props));
  }

  render() {
    return (
      <div>{this.props.children}</div>
    );
  }

An element is wrapped in a component.
In option 1, it turns out:
<ul>
    <li>
        <li></li>
    </li>
</ul>

At 2:
<ul>
    <div>
        <li></li>
    </div>
</ul>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KnightForce, 2017-05-10
@KnightForce

The problem is here:
Namely, in Object.assign({}, props)
Since children are in props , and here I am forwarding it to a descendant (which, in fact, is in children ) - duplication occurs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question