Skip to main content

Reconciliation, Identity, and State Preservation

React compares a new render description with the previous committed React tree and decides what identities can be preserved, replaced, or removed.

Conceptual model: the stable application-facing rules matter more than private reconciler fields or exact heuristics.

Reconciliation is not DOM diffing​

React reasons about the React tree before the renderer mutates the host
Previous committed React tree
accepted identities + state
New render description
elements returned by components
Accepted React work
reuse Β· replace Β· insert Β· remove
Host changes
DOM / native platform mutations

A component can render without causing a DOM mutation, and state preservation depends on React identity rather than DOM resemblance.

Identity controls state preservation​

Component identity is shaped by type, position, and key
Type
Card vs Editor vs Profile
Position
logical sibling/tree position
Key
explicit sibling identity

When the identity is preserved, React can preserve state for that subtree. When identity changes, state below that point is recreated.

Same type + same position can preserve state​

function Panel({ compact }) {
return compact
? <Card density="compact" />
: <Card density="comfortable" />;
}
Changing props is not the same as changing identity
Card
density="compact"
Same Card identity
density="comfortable" Β· state preserved

Different type resets the subtree​

function App({ editing }) {
return <main>{editing ? <Editor /> : <Profile />}</main>;
}
Type replacement creates a new subtree identity
Profile
old local state
Editor
new local state

Position matters among siblings​

Without stable keys, reordering sibling components can make identity follow position rather than domain meaning.

Unkeyed sibling identity is position-sensitive
Before
position 1: Editor Β· position 2: Sidebar
After reorder
position 1: Sidebar Β· position 2: Editor

If those siblings represent durable domain entities, give React stable keys that represent those entities.

Keys are explicit sibling identity​

{messages.map(message => (
<Message key={message.id} message={message} />
))}
Is this a good key?
Stable domain ID unique among siblingsYes β€” preferred
Array index for reorderable/insertable dataRisky β€” identity follows position
Math.random() / freshly generated valueNo β€” forces a new identity every render
useId()No β€” useId is for DOM relationships, not list identity

Keys only need to be unique among siblings, not globally across the application.

Keys can intentionally reset state​

<Chat key={recipient.id} recipient={recipient} />
Changing a key is an intentional identity reset
Chat key=A
Toolbar state Β· draft state Β· preview state
Chat key=B
fresh subtree state

Key placement is architectural: a key too high resets too much; a key too low can preserve state that belongs to the previous domain identity.

Nested component definitions create unstable types​

function Dashboard() {
function SearchBox() {
const [query, setQuery] = useState('');
return <input value={query} onChange={e => setQuery(e.target.value)} />;
}

return <SearchBox />;
}

Each Dashboard render creates a new SearchBox function identity. Prefer stable module-level component definitions.

Render attempts and commits are different​

A rendered candidate is not necessarily the UI users see
1Update requested
2Render candidate
3May suspend/restart/abandon
4Accepted work commits

This is why render must be pure and why render logs are not proof that a result committed.

Identity debugging checklist​

Why did local state reset unexpectedly?
Component type changedDifferent identity β€” expected reset
Sibling position/order changedInspect keys and tree structure
Key changedVerify whether reset is intentional
Component defined inside another renderMove definition to stable module scope
Whole ancestor remountedFind the first identity change above the state owner

Senior mental model​

Stable contract
type/position/key identity Β· state preservation/reset Β· render/commit distinction
Implementation detail
exact internal diff heuristics Β· Fiber fields Β· flags Β· lane representation

Reason from the stable behavior React documents. Treat internal implementation details as a way to understand Reactβ€”not as application dependencies.