Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HOC call callback when props changed
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