Skip to content

Instantly share code, notes, and snippets.

@gimelfarb
Created April 13, 2019 23:46
Show Gist options
  • Select an option

  • Save gimelfarb/af930f5781c4e1515706f6c86996dae5 to your computer and use it in GitHub Desktop.

Select an option

Save gimelfarb/af930f5781c4e1515706f6c86996dae5 to your computer and use it in GitHub Desktop.
Recursive React HOC
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