How React renders
Overview
You’ve written components, but what actually happens between your JSX and pixels on the screen? Understanding React’s rendering process explains a lot of its behaviour — why state works the way it does, why components must be pure, and why React is fast even though it “re-runs everything”.
Think of React as a waiter in a restaurant: it takes orders (triggers), gets the kitchen to prepare them (render), and serves the dishes (commit).
Step 1: Trigger
There are only two reasons a component renders:
- The initial render — when the app starts,
createRoot(…).render(<App />)triggers it. - A state update — when a component (or one of its ancestors) calls a state setter like
setCount(1).
Nothing else re-renders a component. Changing a regular variable, mutating an object, or changing a prop’s value from outside without a state update does not make React render again.
Step 2: Render
“Rendering” means React calls your component functions to find out what should be on screen.
- On the initial render, React calls the root component, then every component it returns, and so on down the tree.
- On a re-render, React calls the component whose state changed — and, by default, every component inside it, because their output may depend on the new state.
The result of rendering is a tree of React elements: lightweight JavaScript objects describing the UI ({ type: 'h1', props: { children: 'Clicked 3 times' } }). Creating these objects is cheap — far cheaper than touching the DOM.
This is why components must be pure: React may call them at any time, more than once (StrictMode does it twice deliberately), or start a render and throw it away. If rendering changed things outside the component, those repeated calls would cause bugs.
Step 3: Commit
After rendering, React commits the changes to the DOM:
- On the initial render, it creates all the DOM nodes and inserts them with
appendChild. - On re-renders, it compares the new element tree with the previous one (this comparison is called reconciliation) and applies the minimum set of DOM operations needed.
function Counter({ count }) {
return (
<div>
<h1>Clicked {count} times</h1>
<input placeholder="Notes" />
</div>
);
}When count goes from 2 to 3, React sees that the <div>, <h1> and <input> are all the same types in the same places. It changes only the text inside the <h1>. The <input> DOM node is untouched — so whatever the user typed in it, its focus and its cursor position all survive.
How React compares trees
React’s comparison uses two simple rules that make it fast:
- Different element types produce different trees. If a
<div>becomes a<section>, or<CourseCard>becomes<CourseRow>, React destroys the old DOM (and any state inside it) and builds new nodes from scratch. - Keys identify items in lists. When rendering arrays, the
keyprop tells React which item is which, so it can move DOM nodes instead of rebuilding them. You’ll see this in Rendering lists and keys.
These rules come back again when you learn about Preserving and resetting state.
Step 4: The browser paints
After React updates the DOM, the browser recalculates layout and paints the new pixels. This step isn’t React — but React’s job is to make the DOM changes as small as possible, so this step is fast too.
“Doesn’t re-rendering everything waste time?”
It sounds wasteful that a state change in App re-renders every component below it. In practice:
- Rendering is just calling functions and creating small objects — usually microseconds per component.
- The expensive work, DOM updates, only happens where something actually changed.
- When a component really is slow to render, you can skip unnecessary renders — and React Compiler does this automatically (see the Performance chapter).
So write simple, readable components first, and optimise only where you measure a problem.
The virtual DOM
You’ll often hear that React uses a virtual DOM. That term refers to the element tree from the render step: an in-memory description of the UI that React compares between renders. The React team rarely uses the phrase today, but the idea is exactly what you’ve just learned — describe the UI as cheap objects, then update the real DOM only where it differs.
Seeing renders yourself
- Put a
console.log('render', props)in a component body to see when it renders (remember: twice in development under StrictMode). - In React DevTools → Components → ⚙ settings, enable “Highlight updates when components render”. Components flash as they re-render.
- In your browser’s Elements panel, DOM nodes that change flash briefly — compare that with how often components render.
Mental model: your components are a description. React keeps the real DOM in sync with the latest description, doing as little work as possible.
Watch React render
Build a Clock component that shows the current time and re-renders every second (use useState and a setInterval inside useEffect — you’ll learn these properly soon; the starter gives you the hook code). Open your browser’s Elements panel and watch: which DOM node flashes as it updates every second? Add a console.log('render') in Clock’s body. Then add an <input> next to the time, type in it, and confirm the text you typed survives every re-render.
import { useEffect, useState } from 'react';
export default function Clock() {
const [now, setNow] = useState(() => new Date());
useEffect(() => {
const id = setInterval(() => setNow(new Date()), 1000);
return () => clearInterval(id);
}, []);
// your code here
}import { useEffect, useState } from 'react';
export default function Clock() {
const [now, setNow] = useState(() => new Date());
useEffect(() => {
const id = setInterval(() => setNow(new Date()), 1000);
return () => clearInterval(id);
}, []);
console.log('render');
return (
<section>
<h1>{now.toLocaleTimeString()}</h1>
<input placeholder="Type here — I survive re-renders" />
</section>
);
}
// In the Elements panel only the text inside <h1> flashes.
// The <input> is the same DOM node every second, so what you typed stays.Summary
- Showing UI takes three steps: trigger (initial render or state update), render (React calls components) and commit (React updates the DOM).
- Rendering means calling your component functions; it doesn’t touch the DOM.
- React compares the new JSX with the previous render and changes only the DOM nodes that differ.
- When a component re-renders, all components it renders re-render too, by default.
- Because only differences are applied, DOM state like focus, selection and typed text is preserved.