4 ms·
useEffect is treating undefined as meaning undefined. It makes perfect sense from inside of useEffect. If there are no dependencies ([]), the dependencies can
by systoll 4y ago
useEffect is treating undefined as meaning undefined.
It makes perfect sense from inside of useEffect.
If there are no dependencies ([]), the dependencies can never change, so React can always reuse the old effect.
If the dependencies aren’t defined, there’s no way to tell if the old effect is OK, so React must always rebuild it.
**
But `useEffect(()=>{})` doesn’t explicitly show that React will get `dependencies = undefined`, and in this case that’s unusually important.
You could have ESlint force you to change that to `useEffect(()=>{}, undefined)`, I suppose…
- bottlepalm 4y agoYes we know how it works. What we’re saying is that it is a cryptic design. Just like the function returned from useEffect is used for unmounting the component. Not intuitive at all.
- darepublic 4y agothere is an eslint rule exhaustive-deps which really helps with this
- systoll 4y agoFor useMemo and useCallback, Typescript and exhaustive-hooks will both tell you to add a dependency array, because without it those hooks do nothing. For useEffect, exhaustive-hooks & Typescript say nothing, because a non-memoised Effect is a reasonable thing to write. It’s just not a great thing to write accidentally.