Skip to content

Instantly share code, notes, and snippets.

@oliverbenns
Created November 20, 2017 08:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliverbenns/dcc7f2c5e23a7a63b5a39b40b9a1a75e to your computer and use it in GitHub Desktop.
Save oliverbenns/dcc7f2c5e23a7a63b5a39b40b9a1a75e to your computer and use it in GitHub Desktop.
Hide/Show HOC
import React from 'react';
const visible = isVisible => Component => {
class VisibleComponent extends React.Component {
componentDidMount() {
this.unsubscribe = this.context.store.subscribe(this.handleChange.bind(this));
}
componentWillUnmount() {
this.unsubscribe();
}
handleChange() {
this.forceUpdate();
}
render() {
const show = isVisible(this.context.store.getState());
if (!show) {
return null;
}
return <Component {...this.props} />;
}
}
VisibleComponent.contextTypes = { store: React.PropTypes.object };
return VisibleComponent;
};
export default visible;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment