React useMemo Not Working— Still Re-computing Every Render [Fixed 2026]
Also covers: dependency array mistakes · referential equality · object deps · React.memo · when to skip useMemo
⚡ The Problem
// useMemo recalculates on every render despite dependencies
const sortedList = useMemo(() => {
console.log("sorting...") // logs on every render — memo not working!
return [...items].sort((a, b) => a.name.localeCompare(b.name))
}, [items, filters]) // filters is an object created inline — new ref every render✅ Root cause — unstable reference in dependencies
// ❌ Object dep — new reference every render
const filters = { status: "active", page: 1 } // created inline
useMemo(() => compute(items, filters), [items, filters])
// ✅ Use primitives from the object as deps instead
useMemo(() => compute(items, status, page), [items, status, page])Why useMemo Re-computes Every Render
React's useMemo uses Object.is to compare each dependency between renders. For primitive values (strings, numbers, booleans), this is value comparison. For objects and arrays, this is reference comparison — a new object with the same properties is still a different reference.
If any dependency changes reference between renders — even if its contents are identical — useMemo invalidates the cache and re-runs the factory function.
Correct the Dependency Array
Most common mistake — missing or wrong depsThe dependency array must contain every value used inside the memo factory. Missing deps cause stale results; extra deps cause unnecessary recomputation. The ESLint rule exhaustive-deps catches both automatically.
import { useMemo } from "react"
// ❌ Missing dependency — stale result when userId changes
const userPosts = useMemo(() => {
return posts.filter(p => p.userId === userId) // userId used but not in deps!
}, [posts]) // bug: userId missing
// ✅ All used values in deps
const userPosts = useMemo(() => {
return posts.filter(p => p.userId === userId)
}, [posts, userId]) // both deps included
// ❌ Unnecessary dep — causes recompute when unrelated state changes
const sortedPosts = useMemo(() => {
return [...posts].sort((a, b) => a.date - b.date)
}, [posts, currentUser]) // currentUser not used inside — remove it
// ✅ Only what you actually use inside the factory
const sortedPosts = useMemo(() => {
return [...posts].sort((a, b) => a.date - b.date)
}, [posts])Install eslint-plugin-react-hooks and enable the react-hooks/exhaustive-deps rule. It automatically detects missing and unnecessary dependencies, turning dependency array mistakes into lint errors.
Avoid Objects and Arrays as Dependencies
Object dep created inline — new ref every renderObjects and arrays created during render are new references every time, even if their contents are identical. Use primitive values from them as deps instead.
// ❌ Object dep — {} !== {} — recomputes every render
function ProductList({ filters }: { filters: { status: string; minPrice: number } }) {
const filtered = useMemo(() => {
return products.filter(p => p.status === filters.status && p.price >= filters.minPrice)
}, [products, filters]) // filters is a new object every render from the parent
// ...
}
// ✅ Destructure to primitives — strings/numbers are stable
function ProductList({ filters }: { filters: { status: string; minPrice: number } }) {
const { status, minPrice } = filters // destructure outside useMemo
const filtered = useMemo(() => {
return products.filter(p => p.status === status && p.price >= minPrice)
}, [products, status, minPrice]) // primitives — stable comparison
// ...
}
// ✅ Alternative — memoize the filters object in the parent
const filters = useMemo(() => ({ status, minPrice }), [status, minPrice])
// Now filters is a stable reference — safe to pass and use as depCombine useMemo with React.memo for Prop Memoization
Child still re-renders despite useMemouseMemo alone doesn't prevent child re-renders. The child must be wrapped in React.memo — then it only re-renders when its props change reference. Use useMemo to produce stable props, and React.memo on the child to skip rendering.
import { memo, useMemo } from "react"
// Child wrapped in React.memo — skips re-render if props are same reference
const ExpensiveChart = memo(function ExpensiveChart({
data,
config,
}: {
data: ChartPoint[]
config: ChartConfig
}) {
// Only re-renders when data or config reference changes
return <canvas>{/* chart rendering */}</canvas>
})
// Parent — produces stable props with useMemo
function Dashboard({ rawData, theme }: { rawData: RawPoint[]; theme: string }) {
// Stable transformed data — only recomputes when rawData changes
const chartData = useMemo(() =>
rawData.map(p => ({ x: p.timestamp, y: p.value })),
[rawData]
)
// Stable config object — only recomputes when theme changes
const chartConfig = useMemo(() => ({
theme,
showGrid: true,
animationDuration: 300,
}), [theme])
// ExpensiveChart only re-renders when chartData or chartConfig reference changes
return <ExpensiveChart data={chartData} config={chartConfig} />
}Memoize Expensive Derived Data Correctly
Sorting, filtering, or aggregating large arraysuseMemo is most valuable for computations that are genuinely expensive — sorting or filtering arrays of thousands of items, complex aggregations, or heavy string transformations.
import { useMemo } from "react"
// ✅ Sorting a large list — O(n log n) — worth memoizing
function ProductTable({ products, sortKey, sortDir }: Props) {
const sorted = useMemo(() => {
return [...products].sort((a, b) => {
const cmp = a[sortKey] < b[sortKey] ? -1 : a[sortKey] > b[sortKey] ? 1 : 0
return sortDir === "asc" ? cmp : -cmp
})
}, [products, sortKey, sortDir])
return <table>{sorted.map(p => <ProductRow key={p.id} product={p} />)}</table>
}
// ✅ Multi-step filter + aggregation
function SalesDashboard({ orders, dateRange, region }: Props) {
const stats = useMemo(() => {
const filtered = orders.filter(o =>
o.date >= dateRange.start &&
o.date <= dateRange.end &&
(region === "all" || o.region === region)
)
return {
total: filtered.reduce((sum, o) => sum + o.amount, 0),
count: filtered.length,
average: filtered.length > 0
? filtered.reduce((sum, o) => sum + o.amount, 0) / filtered.length
: 0,
}
}, [orders, dateRange.start, dateRange.end, region]) // primitives from dateRange
return <StatsPanel stats={stats} />
}When NOT to Use useMemo
Over-memoization — adds overhead without benefituseMemo has a cost: React must store the previous value and compare dependencies on every render. For cheap computations, this overhead exceeds the savings. Avoid useMemo for these patterns:
// ❌ Simple calculation — faster without useMemo
const total = useMemo(() => price * quantity, [price, quantity])
const total = price * quantity // ✅ just do it inline
// ❌ String concatenation — trivially cheap
const label = useMemo(() => `${firstName} ${lastName}`, [firstName, lastName])
const label = `${firstName} ${lastName}` // ✅ inline
// ❌ Array of 3 items — sorting is instant
const sorted = useMemo(() => [...items].sort(), [items])
// ✅ Only worth it for large arrays (100+ items)
// ❌ JSX — use React.memo on the component instead
const element = useMemo(() => <MyComponent prop={value} />, [value])
// ✅ Wrap MyComponent in React.memo instead
// ✅ USE useMemo when:
// — Sorting/filtering 100+ items
// — Heavy string parsing or regex
// — Producing stable object references passed to React.memo children
// — Complex mathematical aggregationsPremature memoization is a real problem. Adding useMemo everywhere adds cognitive overhead and can actually slow down your app for simple computations. Profile first with React DevTools Profiler, then memoize only what the profiler shows is slow.
Prevention
- Enable eslint-plugin-react-hooks exhaustive-deps rule — catches wrong dependency arrays automatically
- Use primitives (strings, numbers) as deps, not objects or arrays created inline
- Combine useMemo with React.memo — useMemo stabilises values, React.memo skips child renders
- Profile with React DevTools Profiler before adding useMemo — only memoize proven bottlenecks
- Destructure objects outside useMemo and use individual primitive fields as deps
- Use useMemo for sorting/filtering 100+ items, complex aggregations, or stabilising object refs
Frequently Asked Questions
Why is useMemo still re-computing even with dependencies set?+−
What is the difference between useMemo and React.memo?+−
Should I use useMemo for every expensive calculation?+−
Why does useMemo not prevent child component re-renders?+−
Can I use useMemo with an empty dependency array?+−
Does React Strict Mode affect useMemo?+−
Need Expert Help?
We Build High-Performance React Applications
Softplix engineers optimise React apps with memoization, code splitting, and profiling. Struggling with performance? Let us help.
Talk to an Engineer