Rendering, Reconciliation and Memoisation

What triggers a re-render, how React reconciles the tree, keys and identity, and when memo, useMemo, useCallback or the React Compiler help.

Advanced · about 5 hours · difficulty 4/5

Learn first: Context and Component Composition

To fix slow or buggy React code you need a clear model of rendering. Rendering means React calls your component function to get new JSX; committing means applying the minimal DOM changes. Renders are usually cheap; unnecessary DOM work and heavy computations are what hurt.

What triggers a render

  • The component's state changes.
  • Its parent re-renders (children re-render by default, even with identical props).
  • A context it consumes changes.

Reconciliation and identity

React compares the new tree with the previous one by position and type. Same type at the same position keeps its state; a different type resets it. Keys give list items a stable identity so reordering does not scramble state. Changing a key on purpose is a clean way to reset a component.

Never define a component inside another component: it becomes a new type every render and loses its state.

Memoisation tools

  • memo(Component) skips re-rendering when props are shallowly equal.
  • useMemo caches an expensive computed value; useCallback caches a function identity (useful when passed to memoised children or used as a dependency).
  • The React Compiler can apply this memoisation automatically at build time, reducing manual useMemo/useCallback in codebases that adopt it.

How to optimise

Measure first with the React DevTools Profiler ("highlight updates when components render"). Common real fixes: move state down closer to where it is used, lift expensive children out as children, virtualise long lists, and avoid creating new objects in props for memoised children.

Good enough to move on

You can explain why a component re-rendered, use the Profiler to find a slow render, and fix it with the right tool rather than wrapping everything in memo.

Key concepts

  • Render and commit phases
  • What triggers re-renders
  • Reconciliation, position and type
  • Keys and resetting state
  • memo, useMemo and useCallback
  • React DevTools Profiler
  • React Compiler
  • List virtualisation

Mini project

Build a data grid with 5,000 rows, filtering and a live-typing search box. Profile it, then make typing smooth by moving state down, memoising rows and virtualising the list; record before/after timings.

Check yourself

Why does wrapping a child in memo not help if the parent passes `style={{ color: "red" }}` to it?

Free resources