Skip to main content
useMemo is a React Hook that lets you cache the result of a calculation between re-renders.

Parameters

() => T
required
The function calculating the value that you want to cache. It should be pure, take no arguments, and return a value of any type. React will call your function during the initial render. On subsequent renders, React will return the same value if the dependencies haven’t changed. Otherwise, it will call create, return its result, and store it for later reuse.
Array<mixed> | void | null
The list of all reactive values referenced inside the create code. React will compare each dependency with its previous value using the Object.is comparison.
  • If omitted, useMemo will recalculate on every render (defeats the purpose)
  • If [] (empty array), the value is calculated only once
  • If [dep1, dep2], recalculates when dependencies change

Returns

On the initial render, useMemo returns the result of calling create with no arguments. During subsequent renders, it will either return an already stored value from the last render (if the dependencies haven’t changed), or call create again and return the result that create has returned.

Usage

Skipping expensive recalculations

To cache a calculation between re-renders, wrap it in useMemo:

Skipping re-rendering of components

When you pass an object or array to a child component, create it with useMemo to keep the same reference:
This pattern is only useful when the child component is wrapped in memo. Without memo, the child will re-render anyway.

Memoizing a dependency of another Hook

Prevent an effect from running unnecessarily:

Memoizing a function

Use useCallback instead:

Common Patterns

Expensive calculations

Creating objects and arrays

Computing derived state

Preventing unnecessary deep comparisons

TypeScript

Troubleshooting

My calculation runs on every render

Make sure you provided a dependency array:

My useMemo call runs on every render despite dependencies

One of your dependencies might be changing on every render:
Solutions:
  • Move constant values outside the component
  • Use useMemo for the dependency too
  • Depend on individual values instead of objects/arrays

How to tell if a calculation is expensive?

Add a console.time to measure:
If the calculation takes less than 1ms, it’s probably not worth memoizing. The overhead of useMemo itself might be more expensive than the calculation.

Should I add useMemo everywhere?

No! Only use useMemo when:
  1. The calculation is expensive (takes >1ms)
  2. You’re creating objects/arrays passed to memoized components
  3. The value is used as a dependency in another Hook
  4. You’ve measured and confirmed it improves performance

Performance Tips

Don’t optimize prematurely

  1. Write code normally first
  2. Use React DevTools Profiler to find slow components
  3. Add useMemo only where it helps
  4. Measure again to confirm improvement

Common mistakes

When NOT to use useMemo

  • Primitive calculations (math, string operations)
  • Creating elements (<div>, <Component />)
  • Simple array operations on small arrays (less than 100 items)
  • Object/array literals that aren’t passed anywhere

useMemo vs useCallback