AI summary by TwinMind

You Probably Shouldn't Use React.memo()

Theo - t3․gg

10m 16s

React Performance Context

  • React has performance optimization pieces built-in, including React.memo
  • React.memo prevents component/component tree updates when unnecessary
  • React.memo is most misunderstood React feature - used incorrectly more than correctly
  • TK Dodo (Dominic) - React Query contributor, wrote article on React.memo misuse

Component Composition vs Memoization

  • Existing resources: Dan Abramov's "Before you memo", Kent's "Simple trick to optimize renders"
  • Component composition is React's natural mental model
  • Works well with server components
  • Moving state down or lifting content up solves problems naturally
  • Children passed as props maintain same reference - React can short-circuit renders

React.memo Problems - Too Easy to Break

  • React renders component tree top-down for all children once started
  • No way to stop render once started (mostly good thing)
  • React can abort renders if sees same reference
  • React.memo compares props with Object.is
  • Works fine with no props or primitive values (numbers, strings)
  • Breaks with functions, objects, arrays as props

Memoization Breaking Examples

  • Style Object Issue: style={{backgroundColor: 'blue'}} creates new object each render
    • Comparison fails, component re-renders every time
    • Fix: wrap in useMemo, but no guarantees from above/below
  • Children Breaking Memoization: JSX is syntactic sugar for React.createElement
    • Creates new object every render
    • Need to memoize JSX render and pass memoized element as child
  • Prop Contamination: One unmemoized prop breaks entire memoization chain
    • Empty objects/arrays as default values destroy performance
    • Separates slow component from what makes it slow

Real-World Example - Multiple Tables Problem

  • Page with 5 big tables + summary box
  • When one table changes, everything renders (slow)
  • Initial solution: wrap tables in memo + useCallback functions
  • Problem: state too high up, one prop can break all optimizations

Alternative Solutions

External State Management (Zustand Example)

  • Store state outside React component tree
  • Surgically trigger re-renders only where needed
  • Similar to React Query approach
  • Create store with actions (addToTable, removeFromTable)
  • Export custom hooks: useTable1Data, useTable2Data, useSummaryData
  • Components subscribe only to state they need
  • Avoids top-down renders, no memoization pitfalls

Component Composition

  • Best option when possible
  • Not always feasible depending on requirements

Future Solutions

  • Records and Tuples: ECMAScript proposal (stage 2)
    • Would help with arrays/objects, not functions
  • React Forget: React team's compiler
    • Automatically memoizes everything
    • Performance optimizations without error surface

Solution Ranking

  1. Component composition (best, not always possible)
  2. External state managers (better than memo)
  3. Memoization (worst option - hard to read, easy to break)

TwinMind

Get summaries like this for your own meetings

TwinMind takes notes in the background during meetings, lectures, and calls, then writes the summary for you.

Free plan to get started

No bot joins your calls

Phone, desktop & Chrome apps

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome