Created
November 20, 2017 08:31
-
-
Save oliverbenns/dcc7f2c5e23a7a63b5a39b40b9a1a75e to your computer and use it in GitHub Desktop.
Hide/Show HOC
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
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