Full answer
Every render of a component creates new objects and functions. For the render itself that is cheap. It becomes a problem in two cases: a calculation is slow, or a new reference makes a memoized child or a dependency array think something changed.
useMemo(() => compute(a, b), [a, b]) runs the function and stores the result. On the next render it returns the stored value unless a or b changed. It is for expensive calculations and for keeping an object or array stable.
useCallback(fn, deps) stores the function itself. It is the same as useMemo(() => fn, deps). It is for a function that is passed to a memoized child or used as an effect dependency.
On its own, useCallback does nothing for speed. The parent still re-renders. The benefit appears only when the child is wrapped in memo, because memo skips the child when all props are shallow-equal. A new function every render would break that check.
Do not wrap everything. Each hook costs memory and comparison work, and the code gets harder to read. A sensible order is: measure first with the React Profiler, then fix the slow spot. Often the better fix is to move state down, split a component or pass children.
React Compiler, which became stable in late 2025, can add this memoization automatically at build time for code that follows the rules of React. Many projects still contain manual hooks, and interviews still ask the difference, but newer code relies on the compiler more.
The caches are a performance hint, not a promise. React may discard cached values, so the code must stay correct without them.
import { memo, useCallback, useMemo, useState } from 'react';
const students = [
{ id: 1, name: 'Asha', marks: 82 },
{ id: 2, name: 'Ravi', marks: 47 },
{ id: 3, name: 'Meera', marks: 91 },
];
const Roster = memo(function Roster({ rows, onSelect }) {
console.log('Roster rendered');
return (
<ul>
{rows.map((s) => (
<li key={s.id}>
<button onClick={() => onSelect(s.id)}>{s.name}</button>
</li>
))}
</ul>
);
});
export default function Gradebook() {
const [passMark, setPassMark] = useState(50);
const [selected, setSelected] = useState(null);
const [ticks, setTicks] = useState(0);
const passed = useMemo(() => {
console.log('filtering');
return students.filter((s) => s.marks >= passMark);
}, [passMark]);
const handleSelect = useCallback((id) => setSelected(id), []);
return (
<>
<button onClick={() => setTicks((t) => t + 1)}>Unrelated ticks: {ticks}</button>
<button onClick={() => setPassMark(60)}>Raise pass mark</button>
<p>Selected: {String(selected)}</p>
<Roster rows={passed} onSelect={handleSelect} />
</>
);
}The tick button re-renders Gradebook but not Roster, and does not repeat the filter, because both references are stable. Console output: the first pair of lines comes from mounting, the second pair from raising the pass mark. The tick button re-rendered Gradebook but added no lines. filtering Roster rendered filtering Roster rendered
Key points
- useMemo caches a value. useCallback caches a function. useCallback(fn, deps) equals useMemo(() => fn, deps).
- They only pay off for expensive work or when a stable reference prevents a child re-render or an effect run.
- useCallback is useful together with memo. Alone it does not stop re-renders.
- Measure with the Profiler before adding them.
- React Compiler can apply this memoization automatically.
Common mistakes
- Adding useCallback to every handler even when the child is not memoized.
- Missing or wrong dependencies, which returns stale data.
- Putting side effects inside useMemo.
- Memoizing cheap calculations where the hook costs more than the calculation.
- Passing an inline object or array to a memoized child and defeating memo.
Follow-ups
- Why does memo fail if the parent passes an object literal as a prop?
- What does React Compiler change about when you need these hooks?
- How would you decide that a calculation is expensive enough to memoize?
Tips
- State the pair clearly: useMemo caches a value, useCallback caches a function. Then talk about when it matters.
- Mention "measure first". It signals practical experience.
- Link useCallback to memo explicitly, since that is the most common follow-up.