Created
June 16, 2019 09:31
-
-
Save ninetails/0a97f8899a537c46dd162e9d36911091 to your computer and use it in GitHub Desktop.
function to create High Order Component factories
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
// source: https://reactjs.org/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debugging | |
export function getDisplayName (Component) { | |
return Component.displayName || Component.name || 'Component' | |
} | |
export const createHOC = Component => params => BaseComponent => { | |
const WrappedComponent = props => ( | |
<Component {...params}> | |
<BaseComponent {...props} /> | |
</Component> | |
) | |
/* prettier-ignore */ | |
WrappedComponent.displayName = `with${getDisplayName(Component)}(${getDisplayName(BaseComponent)})` | |
// eslint-disable-next-line react/forbid-foreign-prop-types | |
if (BaseComponent.propTypes) { | |
WrappedComponent.propTypes = BaseComponent.propTypes | |
} | |
return WrappedComponent | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment