Lesson 163 of 296
Rendering, re-rendering and reconciliation
What you will learn
- What "rendering" means in React
- The render phase and the commit phase
- What causes a re-render
- How reconciliation (diffing) works
- What happens step by step when setState is called
- StrictMode
On this page
Rendering is calling your component
In React, rendering means calling your component function to get JSX: a description of what the UI should look like. It does not necessarily mean changing the DOM.
Two phases
| Phase | What happens | Rules |
|---|---|---|
| Render phase | React calls components and builds the new tree of elements, then compares it with the previous tree | Must be pure: no side effects (no fetching, no changing outside variables, no DOM changes). React may call it more than once or pause it. |
| Commit phase | React applies the minimal set of changes to the real DOM, then runs effects (Lesson 166) | Side effects happen after this |
This is why a component body must be a pure calculation from props and state (Lesson 38): same inputs, same output.
What causes a re-render?
A component re-renders when:
- Its state changes (via a setter).
- Its parent re-renders: by default, all children re-render too, even if their props did not change.
- A context it uses changes (Lesson 169).
Props changing is not a separate trigger: props only change because the parent re-rendered.
Re-rendering is usually cheap: it is just calling functions. React only touches the DOM where something actually changed. Optimise only when it becomes a measurable problem (Lesson 172).
Reconciliation: how React compares trees
The process of comparing the new element tree with the previous one is called reconciliation (or diffing). Comparing two arbitrary trees perfectly is too slow, so React uses two simple rules, which make it fast (roughly O(n)):
Rule 1: different type means a different thing. If an element changes type (<div> to <section>, or <ProductList> to <Spinner>), React destroys the old subtree (including all state inside) and builds a new one.
Rule 2: keys identify children. Among siblings, React matches old and new children by key (Lesson 162), or by position when there are no keys.
If the type and key are the same, React keeps the component (and its state) and only updates changed attributes.
The plain-object tree of elements that React compares is often called the virtual DOM. The real DOM is only updated in the commit phase.
Consequence: do not define components inside components
function Parent() {
function Child() { return <input />; } a NEW function every render
return <Child />;
}Each render creates a new Child function, which React sees as a different type (rule 1). So Child is destroyed and recreated on every render, and the input loses its text and focus. Define components at the top level of the file.
Step by step: what happens when you call setState
A common interview question: "Explain what happens when setState is called."
- Your handler calls
setCount(5). - React stores the update in a queue for that component and schedules a re-render. The current function keeps running with the old snapshot (Lesson 160).
- Other updates in the same event are batched with it.
- React re-renders: it calls the component again;
useStatenow returns 5. It also re-renders the component's children (unless they are memoised, Lesson 172). - Reconciliation: React compares the new element tree with the previous one.
- Commit: React applies the minimal DOM changes. The browser then paints.
- Effects run after the commit:
useLayoutEffectbefore the browser paints,useEffectafter (Lesson 166).
If the new state is the same as the current one (by Object.is), React can skip re-rendering that component.
Concurrent rendering (brief)
Since React 18, React can interrupt rendering work to keep the page responsive (for example, letting a typing update go first while a slow list renders in the background, Lesson 172). This is why render functions must be pure: React may start a render and throw it away.
StrictMode
import { StrictMode } from "react";
function Root() {
return (
<StrictMode>
<Page />
</StrictMode>
);
}In development only, StrictMode deliberately renders components twice and runs effects twice (mount, unmount, mount) to expose impure rendering and missing effect clean-ups. It has no effect in production. If you see a log or a request twice in development, StrictMode is often the reason; the fix is to make the code correct, not to remove StrictMode.
How to say it in an interview
"Rendering means React calling components to get a description of the UI; the render phase must be pure, and the commit phase applies the minimal DOM changes and then runs effects. A component re-renders when its state changes, its parent re-renders, or a context it reads changes. Reconciliation compares the new tree with the old using two rules: a different element type replaces the subtree, and keys match siblings. When setState is called, React queues and batches the update, re-renders with the new value, diffs, commits the DOM changes, and runs effects. StrictMode double-invokes rendering and effects in development to catch bugs."
Key words
| Word | Meaning |
|---|---|
| Render | Calling a component to get its UI description |
| Commit | Applying changes to the real DOM |
| Reconciliation | Comparing new and old element trees |
| Virtual DOM | The tree of plain objects React compares |
| Pure render | Rendering with no side effects |
| StrictMode | A development tool that double-invokes code to find bugs |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.