Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass props/context to dynamic childrens in react?

I am using react, and I am trying to pass props/context to my dynamic childrens, by dymamic childrens I mean childrens are render using

{this.props.children}

How can I pass to this children (In my code I know it's type) context/props?

In this jsbin there is an example that it dosen't work on dynamic childrens. http://jsbin.com/puhilabike/1/edit?html,js,output

like image 793
Yosi Avatar asked Feb 28 '15 11:02

Yosi


1 Answers

Though @WiredPrairie's answer is correct, the React.addons.cloneWithProps is deprecated as of React v0.13RC. The updated way to do this is to use React.cloneElement. An example:

renderedChildren = React.Children.map(this.props.children, function (child) {
 return React.cloneElement(child, { parentValue: self.props.parentValue });
});
like image 88
codematix Avatar answered Sep 20 '22 18:09

codematix