Created
February 23, 2018 02:25
-
-
Save og24715/2b42bbf1316133fd6f1cf8129bed9aaa to your computer and use it in GitHub Desktop.
How to clone React element with new props.
This file contains hidden or 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
render() { | |
return ( | |
{React.Children.map( | |
this.props.children, | |
(child, index) => { | |
switch (typeof child) { | |
case 'string': | |
return child | |
case 'object': | |
return React.cloneElement(child, newProps); | |
// or | |
// return <child.type {...child.props, ...newProps} /> | |
default: | |
return null | |
} | |
}, | |
)} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment