Skip to content

Instantly share code, notes, and snippets.

@ninetails
Created June 16, 2019 09:31
Show Gist options
  • Save ninetails/0a97f8899a537c46dd162e9d36911091 to your computer and use it in GitHub Desktop.
Save ninetails/0a97f8899a537c46dd162e9d36911091 to your computer and use it in GitHub Desktop.
function to create High Order Component factories
// 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