What is the difference between useMemo and useCallback?

React performance · Freshers and experienced

Model answer

useMemo caches the result of a calculation between renders: useMemo(() => expensive(a), [a]). useCallback caches a function itself: useCallback(() => doIt(a), [a]), which is the same as useMemo(() => () => doIt(a), [a]).

Use them for expensive calculations or to keep stable references passed to memoised children or used in effect dependencies. They are not free, so do not wrap everything.

Learn this topic

React Performance: memo, useMemo, useCallback and When Not to Use Them in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions