Skip to content

Instantly share code, notes, and snippets.

@didacus
Last active June 12, 2019 14:03
Show Gist options
  • Save didacus/a5febad68d997275cd61d7fe363914b9 to your computer and use it in GitHub Desktop.
Save didacus/a5febad68d997275cd61d7fe363914b9 to your computer and use it in GitHub Desktop.
ReactJS — Watch for changes and fetch
// New
const [clear, setClear] = useCycle(false, true)
useEffect(() => {
//props to watch for changes
const watchlist = ["clear"]
if (watchlist.some(prop => props[prop] !== clear)) {
console.log("clear is", clear, "props.clear is", props.clear)
}
})
// Old
componentDidUpdate(prevProps, prevState) {
//props to watch for changes
const watchlist = ["usersAmount", "gender"]
if (watchlist.some(prop => prevProps[prop] !== this.props[prop])) {
this.fetchJSON(`https://randomuser.me/api/?results=${this.props.usersAmount}&gender=${this.props.gender}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment