Memoization in React
Мемоизация в React
React can reuse a computed value or a function reference when its inputs stay the same. Memoization can also let React skip rendering a child when its props haven't changed.
Also known as: useMemo, useCallback, React.memo
What memoization saves
A React component can render again even when only one of its inputs changes. Typing into a search box, for example, might also re-render a nearby chart whose data hasn't changed. Memoization gives React a way to reuse work instead of repeating it. The key question is what you want to reuse: a calculated value, a function reference, or a component's previous render result.
Pick the tool based on the work
useMemocaches a value. For example,useMemo(() => groupOrders(orders), [orders])can reuse the grouped orders instead of running the calculation again.useCallbackcaches a function reference.useCallback(handler, [orderId])can return the same function across renders. It doesn't call the handler or cache what the handler returns when it runs.React.memowraps a component. It lets React skip rendering that component when its parent renders and its props haven't changed. The component can still render when its own state changes or when a context it reads changes.
The hooks compare each dependency with its previous value using Object.is. By default, React.memo does the same for each prop. For objects, arrays, and functions, what matters is whether it's the same reference, not whether the contents look identical. A single prop with a fresh reference can prevent a child from skipping a render.
Example: keep a handler stable
import { memo, useCallback } from 'react';
const SaveButton = memo(function SaveButton({ onSave }) {
return <button onClick={onSave}>Save</button>;
});
function Editor({ documentId, save, title }) {
const handleSave = useCallback(
() => save(documentId),
[save, documentId]
);
return <>
<h2>{title}</h2>
<SaveButton onSave={handleSave} />
</>;
}
If only title changes, React can reuse handleSave and skip rendering SaveButton, provided save itself has a stable reference. If the caller supplies a new save function on every render, handleSave changes too. Memoizing a tiny button is unlikely to pay off; this example isolates what would matter for a more expensive child.
Measure before adding memoization
Use the React DevTools Profiler to find slow interactions and compare them before and after a change. Memoization is most useful when work is expensive and its inputs frequently stay the same. Cheap calculations often aren't worth the extra comparisons, cache storage, and code. Projects using React Compiler may already get some of these optimizations automatically.
List every reactive value used by the calculation or callback in its dependencies. Leaving out documentId in the example could save the wrong document because the function still closes over the old ID. Keep useMemo calculations pure: don't start requests or mutate props in them. React can discard cached values, so the app must remain correct without the cache.
What interviewers ask
You might be shown a memoized child and asked why it keeps rendering. Check for state or context changes first, then trace the props from the parent. For example, options={{ pageSize: 20 }} creates a new object each time. Passing pageSize={20} removes the reference problem without another hook.