Last active
August 27, 2017 13:28
-
-
Save erasmo-marin/9d864f8f90f0e206d3c076ad8a9532aa to your computer and use it in GitHub Desktop.
withRegistry HOC
This file contains 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 React from 'react'; | |
import PropTypes from "prop-types"; | |
const getDisplayName = (WrappedComponent, defaultName="Unknown") => { | |
return WrappedComponent.displayName || WrappedComponent.name || defaultName; | |
} | |
const withRegistry = (Child) => { | |
class WithRegistry extends React.Component { | |
static contextTypes = { | |
registry: PropTypes.object.isRequired | |
}; | |
static displayName = `WithRegistry(${getDisplayName(Child, 'WithRegistryChild')})`; | |
render() { | |
return <Child {...this.props} registry={this.context.registry}/> | |
} | |
} | |
return WithRegistry; | |
} | |
export default withRegistry; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment