Capítulo 59 de 116
useMemo(calculateValue, dependencies) caches the result of an expensive calculation across renders, only re-running it when a listed dependency actually changes — the value-caching counterpart to useCallback's function-caching.
const cached = useMemo(() => computeExpensiveValue(a, b), [a, b]) — on the first render, React runs the calculation and stores both the result and the dependency values; on later renders, if a/b are unchanged (via Object.is), React returns the cached result without re-running the function at all.memo-wrapped child or an Effect dependency doesn't treat it as "changed" on every render even when its contents didn't change.useMemo itself has memory and comparison overhead — wrapping a genuinely cheap calculation gains nothing and adds complexity. The default should be not memoizing until profiling shows a real cost.useMemo.const visibleTodos = useMemo(
() => todos.filter(todo => todo.text.includes(filterText)),
[todos, filterText]
);
todos or filterText actually change, not on every unrelated re-render of the parent.useMemo is a targeted fix for a proven performance problem, not a default wrapper.useEffect/useCallback.memo/Effect-dependency churn from a fresh reference every render.useMemo usage.