How React Renders and Updates the Tree
From the JSX element to the visible pixel – what exactly happens between a state change and the redrawn screen. Render and commit as separate phases, the comparison of two trees and why React almost always touches only a tiny part of the DOM.
How React Renders and Updates the Tree
setCount(count + 1);
After this line, nothing at all happens in the browser at first. No node is replaced, no pixel redrawn. React initially just calls JavaScript functions – the components – and collects what they return. Whether a DOM node has to be touched at all is only decided afterward, in a separate step. That a component function runs and that the browser gets something to do are two different processes – and most misunderstandings about React performance dissolve at this separation.
The article JSX Precisely: The Rules Behind the Markup was about the fact that a JSX element is ultimately just an object – a description of what should be on the screen. Here the path continues: how does this description become a real DOM, and what happens to it when the state changes? Rendering is a computation in JavaScript, changing the DOM a write operation in the browser. React deliberately keeps the two apart.
Between a state change and a visible DOM change lie several clearly separated stages.
From update to DOM
- Trigger · state, props or context make work necessary
- Render · React calls components and produces the next element structure
- Reconcile · old and new structure are compared by type and key
- Commit · necessary changes are applied to the DOM and effects
The separation explains why calling a component again doesn’t automatically mean a complete rebuild of the DOM.
Three steps: trigger, render, commit#
React displays a UI in three steps. You can picture them like an order in a restaurant kitchen: someone places the order, the kitchen cooks, the waiter brings the dish to the table.
flowchart LR Trigger["1. Trigger<br/>initial or setState"] --> Render["2. Render<br/>call components"] Render --> Commit["3. Commit<br/>minimal DOM change"] Commit --> Paint["Browser repaints"]
The first step is the trigger. There are exactly two occasions. At application startup, it happens once, when you create the root and pass in the top-level component. After that, a re-render is only triggered by a state change – always through the setter function from useState or a corresponding mechanism. Without a state change, React re-renders nothing. That’s important: a click alone does nothing; only when the click changes the state does the machine get going.
import { createRoot } from "react-dom/client";
const root = createRoot(document.getElementById("root"));
root.render(<App />);
Rendering means: React calls your functions#
The second step, rendering, is the one that is misunderstood the most. In React, rendering doesn’t mean painting something on the screen. Rendering means: React calls your component functions to find out what should be displayed. The first time, it calls the root component. On a re-render, it calls the component whose state has changed.
And this call is recursive. If a component returns other components, React calls those too, and their children as well, until there are no more nested components. At the end of this pass there’s no DOM but a new tree of React elements – the same description objects from the JSX article, just the whole tree of them.
flowchart TB App["App()"] --> Liste["InvoiceList()"] App --> Kopf["Header()"] Liste --> Z1["Row() #1"] Liste --> Z2["Row() #2"] Z1 --> Betrag1["Amount()"] Z2 --> Betrag2["Amount()"]
For this pass to be reliable, React requires a property I already emphasized in earlier articles: rendering has to be a pure computation. The same props and the same state must produce the same description, and nothing that already existed may be changed during rendering. Only under this condition can React safely repeat rendering, defer it or discard it in favor of a later result – all things the modern, concurrent version of React actually does.
Reconciliation: comparing two trees#
Now comes the core that explains why the whole thing is fast. After a re-render, React has two trees: the new one, just computed, and the previous one from the last pass. Before anything is written to the DOM, React compares the two. This comparison is called reconciliation, and it follows a few simple rules.
At the same position in the tree, the type of the element decides. If the type is the same – a <div> before, a <div> again now, or the same component before and now – React keeps the existing node and only updates the properties that have changed. If the type is different – a <p> before, a <div> now – there’s no detailed comparison: React tears down the old subtree, throws away its state and builds the new one from scratch.
flowchart LR
subgraph Vorher["Before"]
A1["div"] --> A2["span: 'Hello'"]
end
subgraph Nachher["After"]
B1["div"] --> B2["span: 'World'"]
end
A1 -. "same type:<br/>node stays" .-> B1
A2 -. "only text content<br/>changes" .-> B2This rule has a practical consequence that beginners often stumble over. Because a type change discards the whole subtree, you also lose its state: an input field that used to sit in a <div> and now sits in a <section> is treated as a new element and is empty again. If you want state to be preserved, you have to make sure the same type is at the same position.
Lists are the special case – and this is where keys come in#
For children in a list, the position isn’t enough, because lists change their order. That’s exactly what the key is for, which I already talked about in the article JSX Precisely – and here it becomes clear why it matters so much. When reconciling a list, React doesn’t compare position with position but key with key. An element with the same key counts as the same element as before, no matter where it is now. React then moves the existing DOM node instead of throwing it away and rebuilding it.
That’s why the array index as a key is so treacherous. If you insert an element at the front, every index shifts by one. To React it then looks as if the content of every row had changed – and the state that actually belonged to the third invoice suddenly sticks to the fourth. With a stable domain ID as the key, React recognizes the identity correctly: the inserted element is new, all the others are the same as before and simply move one position further.
Commit: React only touches what has changed#
Only after reconciliation comes the third step, the commit. Now React writes to the real DOM – but only what reconciliation identified as a difference. On the very first render, everything has to be created, and React attaches the created nodes to the page with the usual DOM calls. Every time after that, the decisive sentence from the documentation applies: React only changes a DOM node if there’s a difference between two render passes.
That sounds obvious, but it has visible consequences. When a parent component re-renders but an input field deep inside it stays unchanged, React doesn’t touch that node – and the text you just typed stays put, even though the whole branch was recomputed. That’s exactly the proof that rendering and DOM changes are separate: the component was re-rendered (the function ran), but the DOM wasn’t touched (there was no difference).
sequenceDiagram participant N as User participant R as React participant D as DOM N->>R: setState (state changes) R->>R: call components again (render) R->>R: compare new tree against old one R->>D: write only the differences D->>N: Browser paints the result
After the commit, React is done. What happens next belongs to the browser: it repaints the changed part of the page. This repainting is a separate step, independent of React – another reason why the minimal DOM change is worth so much, because the less changes in the DOM, the less the browser has to repaint.
Why a re-render doesn’t have to be expensive#
With this picture, a widespread misunderstanding dissolves. Many people believe a re-render is automatically slow and optimize wildly against it. But rendering at first only means that a JavaScript function runs again and creates a few objects. That’s rarely the problem. What gets expensive is writing to the DOM and the subsequent repaint – and that’s exactly what React keeps small through reconciliation.
That doesn’t mean rendering is free. If a component high up in the tree re-renders, React calls the whole subtree below it again, even if hardly any DOM changes come out of it in the end. For the vast majority of applications, that’s not an issue. Where it does become measurable, you can memoize a subtree by hand, for example with React.memo: React then skips re-rendering as long as its props haven’t changed. But the order stays the same: measure first, then check whether rendering really is the problem – and most of the time it isn’t.
What to take away#
The whole mechanism boils down to one sentence: on every state change, React computes a new description of the UI, compares it with the old one and writes only the differences to the DOM. The description is cheap, the comparison is clever, and the expensive DOM work stays minimal. The key isn’t a detail here but the information the comparison uses to tell identity from position – and the reason a good choice of key decides whether state is preserved correctly.
With that in mind, you make better decisions in everyday work: you no longer flinch at a re-render, you know why an input field keeps or loses its text, and you understand when a type change or a changing key throws away an entire subtree. For the deeper behavior of state across render boundaries – why state is bound to a position in the tree and when it gets reset – there’s the follow-up article on preserving and resetting state.
Further reading#
- Repository introduction-react (examples to follow along): https://github.com/mikebild/introduction-react
- React, Render and Commit: https://react.dev/learn/render-and-commit
- React, State as a Snapshot: https://react.dev/learn/state-as-a-snapshot
- React, Preserving and Resetting State: https://react.dev/learn/preserving-and-resetting-state
- React, Rendering Lists (keys): https://react.dev/learn/rendering-lists
Revised and translated from German with the help of AI.