Answer the question
In order to leave comments, you need to log in
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)
);
}
renderChildren(props) {
let child = Children.only(props.children);
return cloneElement(child, Object.assign({}, props));
}
render() {
return (
<div>{this.props.children}</div>
);
}
<ul>
<li>
<li></li>
</li>
</ul>
<ul>
<div>
<li></li>
</div>
</ul>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question