Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

Reconciliation

How React decides which parts of the UI to keep, update or recreate: position, element type and keys, and why state survives or resets because of them.

Reconciliation is the step where React compares the tree of elements returned by the latest render with the tree from the previous one and works out the smallest set of changes. It decides, for every spot in the tree, whether to update an existing component instance or destroy it and create a new one.

The rules are simple and worth memorizing, because component state lives in the instance: when React keeps an instance, its state is kept; when React creates a new one, the state starts from scratch. Two things decide it: what is at the same position, and the key.

JSX
// Same type at the same position: update, keep state
<Counter />  ->  <Counter />

// Different type at the same position: destroy and recreate
<Counter />  ->  <p>Hello</p>

Try it

A component keeps its state only while its type and position stay the same. In this demo the counter is wrapped in a div or in a section depending on a checkbox. Count up a few times, then toggle the wrapper: the wrapper type changed, so React throws the old subtree away and the count restarts.

Changing the type resets state

Keys tell React which list item is which. With index keys, React matches items by position, so removing the first row makes every other row "move up" and the input values stay in place. With stable keys the values follow the rows. Type something into each box, then remove "Ann" in both lists.

Index keys versus stable keys

In the first list, after you remove Ann, Bob's row shows the note you wrote for Ann. In the second list the note disappears together with Ann's row.

Syntax

There is no API to call. The algorithm follows these rules:

Situation What React does
Same element type at the same position Keeps the instance. For a DOM element it updates only the changed attributes and children. For a component it re-renders it with the new props and keeps its state.
Different type at the same position Unmounts the old subtree (cleanups run, state is lost) and mounts a new one.
Children in a list with keys Matches children by key, so items can move without losing state.
Children without keys Matches children by index, and warns for dynamic lists.
Same key value, different position Moves the instance; state moves with it.
Changed key on a component Treated as a different component: unmounted and mounted again. See Resetting state with keys.

Complexity: a general tree diff is expensive, so React uses two assumptions to make it linear: elements of different types produce different trees, and keys are stable hints about which children stay the same.

Usage notes

  • Never define a component inside another component. The inner function is a new type on every render, so React destroys and recreates its whole subtree each time, losing state and focus.
  • Keys must be unique among siblings and stable. Use an id from your data. Avoid Math.random() and, for lists that can be reordered, inserted into or filtered, avoid the index.
  • Position counts, even for null. {cond && <A />} before <B /> leaves a placeholder, so B keeps its position when cond flips.
  • Keys are not props. A component does not receive key; use another prop if you need the value.
  • Rendering is not DOM updating. Reconciliation is done during render; the resulting changes are applied in the commit phase (see Render and commit).
  • Keys outside lists are allowed. Putting a key on a single element is the standard way to reset it.