Skip to article content
FrontendXLesson 31 / 40

Memoization Strategies and When to Skip Them

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

advanced · about 55 minutes · 4 exercises

  • Predict
  • Implement
  • Debug
  • Interview

What you will be able to do

#
  • Choose between memo, useMemo and useCallback.
  • Recognize unstable props that defeat memo.
  • Decide when memoization is not worth the cost.

Overview

#

The three memo tools

memo wraps a component and skips re-rendering it when its props are shallow-equal. useMemo caches a computed value. useCallback caches a function identity. All three trade a small cost for skipping work, so they only pay off when the skipped work is expensive or when a stable reference is required.

snippet.tsx
const Row = memo(function Row({ item }: { item: Item }) {
  return <li>{item.label}</li>;
});

const sorted = useMemo(() => [...items].sort(compare), [items]);
const onSelect = useCallback((id: string) => setSelected(id), []);

Unstable props defeat memo

memo compares props shallowly. A new object, array or function created during render is a new reference, so the comparison fails and the child re-renders anyway.

snippet.tsx
// defeats memo: a new object every render
<Row item={{ id: 1, label: 'a' }} />

Composition beats memoization

When a wrapper holds state and renders children, the children re-render with the wrapper. Passing children from the parent means the parent creates the elements, so the wrapper's state changes do not re-render them, with no memo at all.

A custom comparison

memo accepts a second argument to compare props yourself. Use it only when shallow comparison is wrong, and keep it cheap; a slow comparison loses the benefit.

Pitfall: memoizing everything

Blanket memo and useCallback add dependency arrays, memory and comparison cost, and make code harder to read. Measure, then memoize the components the profiler flags.

Pitfall: useCallback without memo

Wrapping a handler in useCallback does not help unless the child it is passed to is memoized. Without memo, the child re-renders regardless of the handler's identity.

The React Compiler

An optional compiler can insert memoization automatically. It relies on pure components and correct dependencies. It does not remove the need to understand identity, because impurity and unstable data still cause bugs.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    Does memo prevent Row from re-rendering?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    const Row = memo(function Row({ item }: { item: Item }) { return <li>{item.label}</li>; });
    function Parent() {
      const [n, setN] = useState(0);
      return (
        <div>
          <button onClick={() => setN(n + 1)}>{n}</button>
          <ul><Row item={{ id: 1, label: 'a' }} /></ul>
        </div>
      );
    }
    Choose the best answer
  2. Exercise 2Implement

    Use composition so the stateful wrapper does not re-render its children.

    starter.tsx
    function Layout() {
      const [n, setN] = useState(0);
      return <div><button onClick={() => setN(n + 1)}>{n}</button><Heavy /></div>;
    }

    Reveal the reference answer
    reference.tsx
    function Layout({ children }: { children: React.ReactNode }) {
      const [n, setN] = useState(0);
      return <div><button onClick={() => setN(n + 1)}>{n}</button>{children}</div>;
    }
    
    // usage: <Layout><Heavy /></Layout>

    Authored expected output: Clicking the button re-renders Layout but not Heavy.

    The parent of Layout creates the Heavy element, so it is not affected by Layout's state. The wrapper re-renders, but its children prop is the same element and Heavy does not re-render.

  3. Exercise 3Debug

    This useCallback does not stop the child from re-rendering. Explain and fix.

    starter.tsx
    const onPick = useCallback((id) => setSelected(id), []);
    <Child onPick={onPick} />

    Reveal the reference answer
    reference.tsx
    const Child = memo(function Child({ onPick }: { onPick: (id: string) => void }) { return null; });
    
    const onPick = useCallback((id: string) => setSelected(id), []);
    <Child onPick={onPick} />

    Authored expected output: Child skips re-rendering when unrelated parent state changes.

    useCallback stabilizes the handler, but a non-memoized child re-renders whenever its parent does. The stable handler only helps once the child is wrapped in memo.

  4. Exercise 4Interview

    Explain when memoization helps and when it hurts, and how you decide.

    starter.tsx
    // memo, useMemo, useCallback, measurement

    Reveal the reference answer
    reference.tsx
    Memoization helps when it skips genuinely expensive work or provides a stable reference that a memoized child or an effect dependency needs. It hurts when the computation is cheap, because the dependency array, memory and comparison cost more than the work saved, and it adds noise that hides the real hot path. The decision is measurement-driven: record the interaction, find the expensive components, memoize those, then re-record to confirm the improvement. Composition, moving state down and virtualizing often remove the need for memoization entirely.

    Authored expected output: A rule based on cost versus savings, plus the measurement that decides it.

    The answer should treat memoization as a targeted tool chosen by evidence, not a default.

Common misconceptions

#
  • memo makes a component never re-render. It skips re-renders only when props are shallow-equal.
  • useCallback speeds up the component that defines it. It only helps if a memoized child receives the function.
  • Memoize every value to be safe. Blanket memoization adds cost and noise; measure first.
  • The React Compiler removes the need to understand identity. Correctness still depends on purity and stable data.

Follow-up practice

#
  • react-compiler-and-purity
  • react-profiling-and-devtools

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.