Skip to content

Instantly share code, notes, and snippets.

@erasmo-marin
Last active August 27, 2017 13:28
Show Gist options
  • Save erasmo-marin/9d864f8f90f0e206d3c076ad8a9532aa to your computer and use it in GitHub Desktop.
Save erasmo-marin/9d864f8f90f0e206d3c076ad8a9532aa to your computer and use it in GitHub Desktop.
withRegistry HOC
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