The useMemo Hook in React
Optimizing expensive calculations with useMemo: caching computed values, referential equality, and when not to over-optimize.
The useMemo Hook in React
useMemo is a React hook that caches (memoizes) the result of a calculation between re-renders, recalculating only when specified dependencies change.
1. Syntax and Example
import { useState, useMemo } from "react";
export function ProductSearch({ products }: { products: Product[] }) {
const [filter, setFilter] = useState("");
// Only re-filters products when 'products' or 'filter' changes:
const filteredProducts = useMemo(() => {
return products.filter(p =>
p.name.toLowerCase().includes(filter.toLowerCase())
);
}, [products, filter]);
return (
<div>
<input value={filter} onChange={e => setFilter(e.target.value)} />
<ul>
{filteredProducts.map(p => (
<li key={p.id}>{p.name}</li>
))}
</ul>
</div>
);
}2. Preserving Referential Equality
In JavaScript, {} !== {}. If you pass an object literal down to a memoized child component, the child will re-render because the object reference changes on every render. useMemo preserves stable object references:
const options = useMemo(() => ({
colorScheme: theme,
showBadge: true,
}), [theme]);3. When NOT to Use useMemo
Do not wrap cheap everyday calculations (like a + b or basic string formatting) in useMemo. The overhead of creating a closure and comparing dependency arrays is often more expensive than the calculation itself.
Related Topics
The useRef Hook in React
Mastering useRef: persisting values without triggering re-renders, DOM element referencing, focus management, and storing timer IDs.
The useCallback Hook in React
Caching callback function definitions with useCallback: preventing unnecessary re-renders of memoized child components and custom hook dependencies.