Created
April 13, 2019 23:46
-
-
Save gimelfarb/af930f5781c4e1515706f6c86996dae5 to your computer and use it in GitHub Desktop.
Recursive React HOC
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
| import getDisplayName from 'react-display-name'; | |
| /** | |
| * Create HOC with recursive Enhancement. | |
| * | |
| * @param {import('react').ComponentType} Component | |
| * @returns {import('react').FunctionComponent} | |
| */ | |
| export function withEnhancement(Component) { | |
| const Enhanced = (props) => { | |
| const {children, ...passProps} = props; | |
| if (children) { | |
| passProps.children = children.map((child) => { | |
| const EnhancedChild = withEnhancement(child.type); | |
| return <EnhancedChild key={child.key} {...child.props} /> | |
| }); | |
| } | |
| // TODO: add your enhancement logic here | |
| return <Component {...passProps} />; | |
| }; | |
| Enhanced.displayName = `withEnhancement(${getDisplayName(Component)})`; | |
| return Enhanced; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment