by doix · 3y ago · 0 comments
> I have a second confession: it’s still not intuitive to me what should and shouldn’t go into the useEffect dependency array, or why...
Come on, this isn't that complicated, I find it hard to take the criticisms seriously when the author hasn't groked React properly.
You don't need to read "multiple articles" about these things, they're explained clearly in the docs
- https://react.dev/reference/react/useMemo#memoizing-a-functi...
- https://react.dev/reference/react/useCallback#how-is-usecall...
- https://react.dev/reference/react/useEffect#specifying-react...
To answer OP's questions, useCallback is
function useCallback(fn, dependencies) {
return useMemo(() => fn, dependencies);
}
As for useEffect, everything that might change between renders, eg props used in the effect, must go in the dependency array.