Before React hooks, I would use componentDidUpdate(prevProps, prevState), and if I wanted to execute given code only when this.state.a had updated, I'd do
if (prevState.a !== this.state.a) {
<...>
}
How can I achieve the same thing in useEffect()?
The useEffect function takes a dependency array where you can mention the states you want to track for change.
useEffect(() => {
//your code here
}, [a])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With