useMemo with invalid dependency causes stale value
Quick summary
Your useMemo dependency array is missing a value the computation uses — the memoized result is stale and does not update when the dependency changes.
Why this happens
The dependency array omits a variable that the memoized computation reads, so the result is not recalculated
An object or array dependency is recreated every render, causing useMemo to recompute too often or not enough
The dependency array is empty but the computation reads props or state that change over time
Minimal example
function ProductList({ products, filter }) {
const filtered = useMemo(
() => products.filter(p => p.category === filter),
[products] // filter missing!
);
return filtered.map(p => <div key={p.id}>{p.name}</div>);
}function ProductList({ products, filter }) {
const filtered = useMemo(
() => products.filter(p => p.category === filter),
[products, filter] // all deps included
);
return filtered.map(p => <div key={p.id}>{p.name}</div>);
}Adding filter to the dependency array ensures the memoized computation reruns whenever the filter changes, keeping the result fresh.
How to diagnose
Log the memoized value and the dependencies to check if the value updates when dependencies change
Use the eslint-plugin-react-hooks exhaustive-deps rule to detect missing dependencies in useMemo
Compare the memoized value with a direct computation to detect staleness
How to fix
Add all values the computation reads to the dependency array of useMemo
Memoize unstable dependencies with useMemo or useCallback so their references stay stable
Remove useMemo if the computation is cheap — memoization overhead may not be worth it for simple calculations
How to prevent
Always include all variables the memoized function reads in the dependency array
Use the exhaustive-deps eslint rule to automatically detect missing or extra dependencies
Related Resources
Related Glossary
Related Lessons
- useMemo
Learn about memoization.