Reconciliation
When the UI description changes, React matches it against the previous one to decide what to keep, update, or replace. That matching process is called reconciliation.
Also known as: согласование, diffing
What reconciliation decides
Suppose a task moves from the bottom of a list to the top while you’re editing its title. The draft should move with the task rather than stay in the bottom row. To make that happen, React needs to recognize that a row in a new position represents the same task. Reconciliation is how React matches the new render output to the existing tree. That match determines which components keep their local state and which DOM nodes can be reused.
How React matches elements
React works within each parent. It uses keys to identify children in a list and falls back to position for children without keys. The element type also has to match: a TaskRow cannot be reused as a TaskSummary just because the two have the same key.
- Same identity and type: React preserves component state. For a DOM element, React can keep the existing node and update its attributes or text as needed.
- Different type: Replacing a
<section>with a<div>at the same spot replaces the subtree, so state held by components inside it is lost. - Different key: React treats the element as new even if its type hasn’t changed. Keys only need to be unique among siblings; they don’t identify components across different parents.
This work happens during rendering. React applies the resulting DOM changes in the commit phase. A component function can run again without any DOM mutation if its output leads to no DOM changes.
Example: keeping a draft with its task
{tasks.map(task => (
<TaskRow key={task.id} task={task} />
))}
Say each TaskRow stores an unsaved title in useState. If you sort the list, the stable task.id tells React which row is which, so the draft stays with the task. With array indexes as keys, prepending a task shifts the data while the keys stay attached to the same positions. React can then reuse a row’s state for the wrong task. Generating a fresh random key on every render causes the opposite problem: React can’t match the rows at all, so it remounts them and discards their drafts.
Practical uses and interview questions
You can deliberately change identity to reset state. For example, <Editor key={documentId} /> gives each newly opened document a fresh editor when the ID changes. Passing a different documentId as an ordinary prop does not reset the editor’s state by itself.
A common interview question is whether a re-render rebuilds the DOM. No: rendering calculates what the UI should look like, reconciliation determines what can be reused and what needs to change, and the commit applies those changes. You’ll often hear diffing used as shorthand for reconciliation. More precisely, diffing is the comparison step. React uses matching rules rather than searching for a globally minimal sequence of DOM operations.