in what case I shouldn't put the empty array [] as dependcies in react useEffect? like
React.useEffect(() => {
if (true) {
doStuff()
}
return () => null;
});
Is above code even valid?
in what case I shouldn't put the empty array [] as dependcies in react useEffect? like
React.useEffect(() => {
if (true) {
doStuff()
}
return () => null;
});
Is above code even valid?
Your code is valid.
undefined is passed as dependencies and it means useEffect runs on every render. So if you want to do some work that shouldn't be run on every render (like adding event listener), you should give a dependency array.
undefined will be passed.