Last active
June 12, 2019 14:03
-
-
Save didacus/a5febad68d997275cd61d7fe363914b9 to your computer and use it in GitHub Desktop.
ReactJS — Watch for changes and fetch
This file contains hidden or 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
// 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