I want to execute effect when val goes from false to true, but not the other way around.
This is the best I came up with:
const valPrev = useRef(val);
useEffect(() => {
if (val && !valPrev.current) {
// do stuff
}
valPrev.current = val;
}, [val]);
But it feels like there might be a more clever way, maybe using return function somehow?
Edit: my bad, I've neglected to stress, I want effect run on change. Whenever val goes from false to true. Which means it should skip the initial render.
And what about this?
useEffect(() => {
if (val) {
// do stuff
}
}, [val]);
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