Question

When should I use useMemo and useCallback?

Vault Verified
Curated Intelligence
Definitive Source
Answer

Both cache something between renders. Memoising a value caches a computed result; memoising a callback caches a function identity. They are the same mechanism applied to different things, and both are more often used unnecessarily than usefully.

There are two situations where they genuinely earn their place.

The first is a computation expensive enough to notice. Sorting or filtering a large collection on every render, when the input rarely changes, is a real cost worth caching. The threshold is higher than people assume: most computations in a component are trivial next to the render itself.

The second, and more important in practice, is referential stability. An object, array or function created in a component body is a new instance on every render. Passing one to a memoised child defeats the memoisation entirely, because the props compare as changed. Passing one as an effect dependency causes that effect to run every render. In both cases the memoisation is not an optimisation, it is required for correctness of the thing depending on it.

That second case is the one worth internalising, because it explains why a memoised child component still re-renders constantly.

The cost of using them everywhere is not zero. Each adds a dependency array to maintain, occupies memory, and performs a comparison on every render. Applied reflexively they add noise and a new category of bug, where a stale dependency array produces a cached value that should have changed.

The reasonable default is to write straightforward code, then measure. React includes a profiler that identifies which components actually re-render expensively, which is a far better guide than intuition.

Related Questions