Skip to content

Instantly share code, notes, and snippets.

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