Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 12 / Topic 5 of 6

When memoization is wasted

Section 4's inline-function mistake generalizes: any prop that is a brand-new array, object, or function literal on every render defeats memo's shallow comparison identically - and memo still pays the cost of running that (useless) comparison every time. examples/05_memoization_wasted.jsx shows the exact same memoized component given a fresh array literal vs. a stable reference:

// examples/05_memoization_wasted.jsx
const STABLE_TAGS = ["spicy", "vegetarian"]; // declared once, outside the component

export function MenuCard() {
  const [note, setNote] = useState("");
  return (
    <div>
      <input aria-label="note" value={note} onChange={(e) => setNote(e.target.value)} />
      <MemoDishTags tags={["spicy", "vegetarian"]} countKey="memoWithInlineProp" />  {/* new array every render */}
      <MemoDishTags tags={STABLE_TAGS} countKey="memoWithStableProp" />              {/* same reference every render */}
    </div>
  );
}

Three keystrokes into the note input really re-render the inline-array tag three more times - renderCounts.memoWithInlineProp reaches 4, identical to what it would be with no memo at all - while the stable-reference tag really stays at 1 (examples/05_memoization_wasted.test.jsx). memo here added a real shallow-comparison cost on every render and prevented exactly nothing. The lesson is not "avoid memo" - it is that memo only ever pays off when the props actually reaching it are stable; wrapping a component is not, by itself, the fix.

Done here? The practice set for Part 12 — including the solutions — is on the part page.

Search the guide