Created
October 18, 2017 13:13
-
-
Save slonoed/f5d45e841b935024d72c6a12741dfa26 to your computer and use it in GitHub Desktop.
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