Skip to content

Instantly share code, notes, and snippets.

@u0078867
Last active February 15, 2016 06:53
Show Gist options
  • Save u0078867/5286ed4cd510f43a0fa8 to your computer and use it in GitHub Desktop.
Save u0078867/5286ed4cd510f43a0fa8 to your computer and use it in GitHub Desktop.
Recursively cloning React children inside DOM tags
var RecursiveChildComponent = React.createClass({
render() {
return <div>
{this.recursiveCloneChildren(this.props.children)}
</div>
},
recursiveCloneChildren(children) {
return React.Children.map(children, child => {
var childProps = {};
if (React.isValidElement(child)) {
childProps = {someNew: "propToAdd"};
}
childProps.children = this.recursiveCloneChildren(child.props.children);
return React.cloneElement(child, childProps);
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment