Skip to content

Lesson 5 of 9 · 28m

Memoization Honesty: useMemo, useCallback, and React.memo

Use memoization as a contract about stable identity, not as a superstition; learn when it helps, when it lies, and when it makes performance worse.

Memoization is the most abused tool in React performance. Developers sprinkle useMemo over code like seasoning, then wonder why nothing improved. Memoization is a cache keyed by identity. It helps when identity stability matters and when recomputing or re-rendering costs more than dependency checking and retention. Everything else is noise, complexity, or worse.

The three tools solve different problems

useMemo       cache a computed value across renders
useCallback   cache a function identity across renders
React.memo    skip a child render when props are shallowly equal

They do not automatically compose. useCallback is pointless if the receiving component is not memoized or does not care about stable identity.

Stable identity is a contract

This example is often justified:

const columns = useMemo(() => buildColumns(tickets), [tickets])
return <DataTable columns={columns} rows={visibleRows} />

It is useful because buildColumns is expensive or because DataTable uses identity to avoid internal work. This is usually theater:

const style = useMemo(() => ({marginTop: 8}), [])

A tiny object calculation is cheaper than the dependency machinery. Memoizing everything teaches teams to worship identity without asking which consumer needs it.

Memoization can leak staleness

Dependency arrays are correctness constraints:

const total = useMemo(
  () => rows.reduce((sum, row) => sum + row.amount, 0),
  [rows],
)

If rows is mutated in place, identity does not change and the total is stale. The calculation stopped running, but so did correctness. Immutable updates are how identity remains meaningful.

React.memo is a boundary, not a blessing

React.memo compares props shallowly. Fresh objects, arrays, functions, and JSX defeat it:

<UserRow user={user} actions={{canEdit, canDelete}} onSelect={() => select(user.id)} />

Prefer changing the API or stabilizing values:

const handleSelect = useCallback((id: string) => select(id), [select])
<UserRow user={user} canEdit={canEdit} canDelete={canDelete} onSelect={handleSelect} />

Do not do this everywhere. A light row does not need a memo contract. A heavy row in a large list probably does.

Composition often beats memoization

Passing children can isolate expensive subtrees because React elements are immutable values:

function SearchPanel({children}: {children: React.ReactNode}) {
  const [query, setQuery] = useState('')
  return <section><input value={query} onChange={e => setQuery(e.target.value)} />{children}</section>
}

When query changes, the same children element identity can be reused. This often solves the problem more clearly than memoizing half the tree.

Field notes

  • useCallback without a memoized consumer is usually dead weight.
  • React.memo compares shallowly; nested object mutation silently defeats it.
  • If render count drops but user latency does not, you optimized a proxy.
  • Correct dependencies are non-negotiable; a fast stale UI is still wrong.

Was this lesson helpful?

Sign in to rate