Skip to content

Instantly share code, notes, and snippets.

@og24715
Created February 23, 2018 02:25
Show Gist options
  • Save og24715/2b42bbf1316133fd6f1cf8129bed9aaa to your computer and use it in GitHub Desktop.
Save og24715/2b42bbf1316133fd6f1cf8129bed9aaa to your computer and use it in GitHub Desktop.
How to clone React element with new props.
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