Last active
February 15, 2016 06:53
-
-
Save u0078867/5286ed4cd510f43a0fa8 to your computer and use it in GitHub Desktop.
Recursively cloning React children inside DOM tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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