All three cache something between renders, and all three compare with Object.is.
memo(Component)skips re-rendering when the parent re-renders and every prop is the same as last time: same keys, each valueObject.is-equal. A second argument,arePropsEqual(oldProps, newProps), replaces that check. It only blocks renders that come from the parent; the component's own state and the contexts it reads still re-render it.useMemo(calculate, deps)returns the cached result until one ofdepschanges, then callscalculateagain.useCallback(fn, deps)returns the cached function until a dependency changes. It'suseMemo(() => fn, deps)with less typing; it never callsfn.
They help in three cases, which the docs list:
- A component that's slow to render re-renders often with the same props: wrap it in
memo. - A value is passed to a
memocomponent or used as a dependency of another hook: keep its identity stable withuseMemooruseCallback, ormemoand the effect see a "change" every render. - A calculation is noticeably slow and its inputs rarely change:
useMemo. The docs suggest around 1 ms, measured in a production build.
Elsewhere they are overhead. memo is all-or-nothing: an inline {} or () => {}, or JSX passed as children, is a new object every render, so the comparison fails and the work is wasted. useCallback on a handler for a plain <button> buys nothing.
Before memoizing, restructure. Move state down into the component that uses it, so its updates don't re-render the slow sibling. Or have the wrapper take the slow part as children: those elements come from its parent and are skipped. And none of these may be needed for correctness: they're optimizations, and React may drop a cache.
With the React Compiler on, it inserts this memoization for you.