HOC call callback when props changed
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
const withPropsChanged = (arePropsChagned, callback) => BaseComponent => { | |
const factory = React.createFactory(BaseComponent) | |
return class Watcher extends React.Component { | |
componentWillReceiveProps(nextProps) { | |
if (arePropsChagned(this.props, nextProps)) { | |
callback(nextProps) | |
} | |
} | |
render() { | |
return factory(this.props) | |
} | |
} | |
} | |
// usage | |
withPropsChanged( | |
(props, nextProps) => | |
props.path != nextProps.path, | |
props => props.selectItem(null) | |
), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment