Created
November 27, 2017 21:08
-
-
Save mathisonian/8878ff5a9b43f9ffcc9fe0cb5ae71f72 to your computer and use it in GitHub Desktop.
Revisions
-
mathisonian revised this gist
Nov 27, 2017 . No changes.There are no files selected for viewing
-
mathisonian created this gist
Nov 27, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ import React from 'react'; import ReactDOM from 'react-dom'; class MyComponent extends React.PureComponent { unwrapChildren() { return this.props.children.map((c) => { if (c => c.type.name && c.type.name.toLowerCase() === 'wrapper') { return c.props.children[0]; } return c; }); } getModifiedChildren() { const unwrapped = this.unwrapChildren(); return React.Children.map(this.props.children, (child, i) => { const c = unwrapped[i]; // The component was wrapped, so return // it with a modified child if (child !== c) { return React.cloneElement(child, { children: React.cloneElement(c, { myCustomProp: 'custom value', }) }); } else { return React.cloneElement(child, { myCustomProp: 'custom value', }); } }) } render () { return ( <div> {this.getModifiedChildren()} </div> ) } } MyComponent.defaultProps = { children: [] }; export default MyComponent;