Home > Net >  Is useMemo(()=>()=>{}, []) the same as useCallback(()=>{}, [])?
Is useMemo(()=>()=>{}, []) the same as useCallback(()=>{}, [])?

Time:12-23

I'm pretty sure the answer is yes. But for those who may have thought useCallback memoizes the result https://reactjs.org/docs/hooks-reference.html#usecallback states

useCallback will return a memoized version of the callback

not the result. nor build a callback that memoizes.

Anyway if useMemo(()=>()=>{...}, []) was mean to workaround a misunderstanding of the useCallback functionality would it be the same as useCallback(()=>{}, []) ?

CodePudding user response:

The doc says they are the same:

useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).

Another thing to keep in mind for both useMemo and useCallback is this:

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.

  • Related