3 ms·
> I have a confession to make: I’m still not exactly sure what the difference between useMemo and useCallback is—or when you should and shouldn’t use them—even
by doix 3y ago
> I have a confession to make: I’m still not exactly sure what the difference between useMemo and useCallback is—or when you should and shouldn’t use them—even though I literally read multiple articles on that exact topic earlier today. (No joke.)
> 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.
- deleted 3y ago[deleted]
- korm 3y agoI'd be frustrated with React too if I worked in team where these things are not well understood. 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-function https://react.dev/reference/react/useMemo#memoizing-a-functi... - https://react.dev/reference/react/useCallback#how-is-usecallback-related-to-usememo https://react.dev/reference/react/useCallback#how-is-usecall... - https://react.dev/reference/react/useEffect#specifying-reactive-dependencies 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.
- leodriesch 3y agoThe linter plugin for hook dependencies is pretty great, I very very rarely need to specify a dependency array different from the one the linter suggests.