How React renders
Rendering is how React turns a state change into a screen update. Knowing the trigger, render and commit phases, the Virtual DOM and reconciliation explains why components re-render, why state survives a re-render and when it gets reset.
31.1 Does React rebuild the whole page on a click?
- The Cart component logs "Cart render" on every render; on page load the console shows Cart render 0.
- Clicking Add calls setItems, the whole Cart function runs again and the console shows Cart render 1.
- But in the DOM only the Items text changes; the heading and the button are not rebuilt.
import { useState } from "react"; export default function Cart() { const [items, setItems] = useState(0); console.log("Cart render", items); const add = () => setItems(items + 1); return ( <div> <h1>My Cart</h1> <p>Items: {items}</p> <button onClick={add}>Add</button> </div> );}> Cart render 0> Cart render 1Screen: only 'Items: 1' updated in the DOM31.2 How it works: the 3 phases
- Trigger: React gets a signal that an update is needed: the first render (mount) or a state setter call like setItems.
- Render: React calls your component function and it returns JSX; the screen is not touched in this step.
- Commit: React compares the new result with the previous one and applies only the needed changes to the real DOM; then the browser paints.
- Analogy: a restaurant: the order comes in (trigger), the kitchen cooks (render), the food is served (commit).
- Step 1
1.Trigger: first load or setItems() is called
- Step 2
2.Render: React calls Cart(), gets new JSX
- Step 3
3.Reconciliation: new tree compared with the previous tree
- Step 4
4.Commit: only the changed text/props applied to the real DOM
- Step 5
5.Browser paints the updated screen
Render does not mean DOM update
31.3 JSX is an object (the Virtual DOM)
- JSX is not HTML; it compiles to a plain JavaScript object with a type and props.
- Every render, a component returns a tree of these lightweight objects: this tree is called the Virtual DOM.
- React compares these objects, not the real DOM nodes.
const items = 2;const el = <p id="count">Items: {items}</p>; console.log(el.type);console.log(el.props);> p> { id: 'count', children: [ 'Items: ', 2 ] }31.4 Reconciliation (diffing)
- After a state change React has two trees: the one from the previous render and the new one.
- It compares them from the top; this comparison is called reconciliation or diffing.
- Same element type at the same position: React keeps the old DOM node and updates only changed text/props.
- Type changed (e.g. a section in place of a div): the old node is removed along with all state inside it and a new node is built.
- In lists, children are matched by key (see the lists video), not by position.
- Goal: the minimum number of real DOM changes.
- Step 1
1.Old tree vs new tree, compared from the top
- Step 2
2.Same type at same position? keep the node, update changed props/text
- Step 3
3.Type changed? remove old node + its state, build a new one
- Step 4
4.List children? match them by key
- Step 5
5.Result: a minimal set of DOM operations for the commit
Goal: minimum DOM changes
31.5 Parent re-render = children re-render
- Post has a views state and renders a child Likes, which has its own likes state.
- On load both log: Post render, Likes render.
- Clicking Views logs Post render and Likes render again, even though Likes got no new prop.
- Rule: when a parent re-renders, all its children re-render by default.
import { useState } from "react";import Likes from "./Likes";export default function Post() { const [views, setViews] = useState(0); console.log("Post render"); const add = () => setViews(views + 1); return ( <div> <button onClick={add}> Views {views} </button> <Likes /> </div> );}> Post render> Likes render> Post render> Likes render31.6 Re-render does not reset state
- Click Likes three times (likes = 3), then click Views: Post and Likes both re-render.
- Likes stays 3, because Likes is the same component type at the same position, so React preserves its state.
- A re-render only means the function is called again; it is not a state reset.
import { useState } from "react"; export default function Likes() { const [likes, setLikes] = useState(0); console.log("Likes render"); const add = () => setLikes(likes + 1); return ( <button onClick={add}> Likes {likes} </button> );}[button] Views 0[button] Likes 3-- after clicking Views --[button] Views 1[button] Likes 331.7 Comparison: what keeps or loses state
| Situation | What React does | State |
|---|---|---|
| Same type, same position | Keeps the DOM node, updates props/text | Preserved |
| Type changes (div -> section) | Removes old node, builds a new one | Reset |
| Component defined inside another component | New function = new type every render | Reset every render |
| List item with a stable key | Matched by key even if it moves | Preserved |
31.8 Common mistakes
- Defining a component inside another component: a new Likes function is created every render, React sees a new type, likes resets to 0 when Views is clicked and inputs lose focus while typing.
- Fix: always define components at the top level of the file.
// Wrong: Likes defined inside Postfunction Post() { const [views, setViews] = useState(0); function Likes() { const [likes, setLikes] = useState(0); return <span>{likes}</span>; } return <Likes />;}// Right: Likes defined at the top levelfunction Likes() { const [likes, setLikes] = useState(0); return <span>{likes}</span>;} function Post() { const [views, setViews] = useState(0); return <Likes />;}- Myth: "the Virtual DOM is always faster than the real DOM". It is an extra step (create objects + compare); careful manual DOM code can be faster. Its benefit is that you describe the UI and React finds the minimum changes.
- Myth: "a re-render repaints the whole page". In the commit only the changed parts are updated.
31.9 When does a component re-render?
- Its own state changes.
- Its parent re-renders.
- A context value it uses changes (covered in the Context video).
- A props change is not a separate reason: props only change because the parent re-rendered.
- Re-renders are normal and fast; do not try to avoid them until the app is actually slow.
31.10 Interview tips
- Say it in three phases: trigger, render, commit.
- Render calls the component and builds a new Virtual DOM tree; reconciliation diffs it against the previous tree.
- Commit applies only the differences to the real DOM; same type at the same position keeps its state.
- Mention that a parent re-render re-renders its children by default, and that render does not mean a DOM update.