useMemo
The React hook that caches the result of a calculation between renders and recalculates only when its dependencies change.
In more detail
Use it for expensive calculations or to keep an object prop stable for a memoized child. React may throw the cache away, so code must still work without it.
Example
const config = useMemo(() => ({ query, limit }), [query, limit]);Related terms
- memoization — Remembering the result of a calculation for given inputs and reusing it when the same inputs come again, instead of recalculating.
- useCallback — The React hook that returns the same function object between renders until one of its dependencies changes.
- dependency array — The array of values given to hooks such as useEffect, useMemo and useCallback that tells React when to re-run them.
- React.memo — A React function that wraps a component so it skips re-rendering when its props are shallowly equal to the previous ones.
Taught in
- Avoiding Effect Antipatterns: Derived State And Events — React 19 & Modern Component Architecture
- Render Diagnostics: Profiling Re-Renders with React DevTools — React 19 & Modern Component Architecture
- React.memo, useMemo, And useCallback: Optimization Costs — React 19 & Modern Component Architecture
- The React Compiler: Automated Memoization Under The Hood — React 19 & Modern Component Architecture