Skip to content
Tutorials Center
My learning

Topics

Lesson 58 of 132 ยท Performance

Rendering in Depth

Understand React's three steps - trigger, render, commit - what causes a re-render, why rendering is not updating the DOM, batching and StrictMode, with logged live examples.

"My component re-rendered. Is that bad?" Almost every performance question in React starts here, and the honest answer is: it depends on what you mean by render. In everyday talk, people use the word for several different things: your function being called, the screen changing, the browser repainting. React separates them carefully, and the whole subject of performance becomes simple once you do the same.

This lesson gives you the precise model. React turns a state change into pixels in three steps: trigger, render, commit. You will learn what can trigger a render, what a render does (and does not do), what the commit does, how updates are batched, and what StrictMode adds in development. Every claim below is backed by a log you can run. With this in your head, memo, reconciliation and profiling stop being recipes and become consequences.

The three steps

Think of a restaurant.

  1. Trigger: a customer places an order. In React, something asks for UI to be (re)drawn: the first root.render(...) call, or a state update.
  2. Render: the kitchen prepares the order. React calls your components and collects the JSX they return, building a description of what the screen should look like. Nothing is shown to anyone yet.
  3. Commit: the waiter delivers the plates. React compares the new description with the previous one and changes only the DOM nodes that differ. Then it runs refs, layout effects and (a bit later) effects.

Two consequences matter everywhere:

  • Render is a pure calculation that may be done more than once, paused, or thrown away. Never put side effects there.
  • Rendering does not mean touching the DOM. A component can render and React can decide, after comparing, that nothing on the screen needs to change.

Step 1: what triggers a render

Only two things start the process:

  1. The initial render, when you call createRoot(...).render(<App />).
  2. A state update in a component (setState from useState, a dispatch from useReducer, a transition, an external store change via useSyncExternalStore).

Props changing is not a third trigger on its own: props only change because a parent rendered, and the parent rendered because of a state update (or, higher up, the initial render). Context works in a similar way: a context provider with a new value triggers its consumers.

The rule: renders cascade downward

When a component renders, React renders all of its children by default, whether or not their props changed. Then their children, and so on, down to the leaves. This is the single most important rule in this lesson. Let us prove it.

Children re-render with their parent

Click the button twice. Our console showed, for the first render, Parent renders, count = 0, PlainChild renders and MemoChild renders. After each click it showed Parent renders, count = 1 (then 2) and PlainChild renders, but no MemoChild renders: the memoized child skipped its render because it had no props that could have changed.

PlainChild receives no props, so nothing about it can have changed, and yet it renders again. React cannot know that without calling it, because a function component could read anything. memo is the opt-out: it tells React "skip me if my props are shallow-equal to last time". It is an optimization you add where measurements say it matters, not a default. The next lesson, memo and referential equality, covers the details.

A re-render of a child does not reset its state. State belongs to a component's position in the tree, and the child is still in the same position. Only unmounting (the component leaves the tree, or its key or type changes) resets state.

Step 2: the render phase, in detail

During rendering React calls your function with the current props and state. Your function returns JSX: a tree of plain objects describing the UI, called elements. React then reads those elements and, for each component element, calls that component too. The result is a complete description of the new UI.

Because the render phase must be repeatable, React demands that components are pure:

  • same inputs (props, state, context), same output;
  • no changing of things that existed before rendering began (variables outside, the DOM, the network).

Creating new objects and arrays inside the render is fine; they belong to this render. Mutating a module-level variable is not. See Pure Components.

StrictMode checks purity for you

In development, <StrictMode> renders every component twice in a row and throws one result away. If your component is pure, you never notice. If it is impure, the duplicate call makes the bug visible. Our editor does not wrap your code in StrictMode, so add it yourself. The component below violates the rule on purpose by changing a variable outside the function.

StrictMode exposes an impure render (fails on purpose)

The console shows Guest renders, guestNumber = 1 and Guest renders, guestNumber = 2, one render per call, and the screen displays Guest #2, not Guest #1.

Rendering the guest twice produced two different numbers, which is exactly how an impure component betrays itself. The fix is to receive the number as a prop or state and never change anything outside the function while rendering. StrictMode also re-runs effects once (set up, clean up, set up again) to check your cleanup code; see Effects. None of this happens in production builds.

Step 3: the commit phase

After rendering, React commits. It compares the new element tree with the previous one (the reconciliation algorithm, see Reconciliation) and applies the minimum set of DOM operations: change this text, set that attribute, insert that node, remove this one. If the output of a render is identical to the last one, React does not touch the DOM at all.

We can watch the DOM itself with a MutationObserver, a browser API that reports every change to the page:

Rendering is not DOM updating

Click Re-render (same output) a few times. Each click logs Clock renders with label "first" (the child renders, as the cascade rule says) but no DOM mutations line follows: the observer saw nothing change. Now click Change label. You get Clock renders with label "second" and DOM mutations: 1, a single change to the text node. Everything else in the page stayed untouched.

This is the answer to "is re-rendering bad?": re-rendering is cheap bookkeeping when the output does not change. It becomes a problem only when the render phase itself is expensive (large trees, heavy calculations in the body), because that runs on the main thread. Reducing unnecessary renders is a CPU optimization for that phase, not a way to avoid DOM work, which React already minimizes.

The order of the commit

Within a commit, things happen in a fixed order. Refs are attached, then layout effects (useLayoutEffect) run synchronously before the browser paints, then the browser paints, then regular effects (useEffect) run. For updates, cleanups run before the new setups. The Profiler component reports once per commit, which makes it handy for counting commits (it works in development builds):

Render phase versus commit phase

Here is what our run printed on mount: 1 render Row, count = 0, 2a ref attached, 3 layout effect, count = 0, Profiler: commit finished, phase = mount, 4 effect, count = 0. After one click: 1 render Row, count = 1, 2a ref detached, 2b layout cleanup, count = 0, 2a ref attached, 3 layout effect, count = 1, Profiler: commit finished, phase = update, 2c effect cleanup, count = 0, 4 effect, count = 1.

The numbers in the messages are labels, not a spec; the printed order is what counts. Read it as phases: the component function runs first (render); everything after belongs to the commit. Layout effects run before the Profiler reports the finished commit, and regular effects run after it. On updates, the old layout cleanup runs before the new layout effect, but the old effect cleanup waits until after the commit, right before the new effect. The ref shows detached then attached on every update only because the inline arrow function is a new function on each render, so React treats it as a different ref; a stable function (or useRef) would not be re-attached.

When setting the same state does nothing (almost)

If you call a state setter with a value identical to the current one (compared with Object.is), React usually bails out: it does not re-render the children and commits nothing. Sometimes it calls the component function one more time first to be sure, before bailing out.

Setting state to the same value

Click Set to 5 (same): in our run the console printed nothing, no App render and no Child render. Click Add one and you get App renders, count = 6 followed by Child renders.

Do not depend on that extra call. It is an internal detail; the contract is only that a bailed-out update does not render the children or change the DOM. This is also why mutating state in place (items.push(x); setItems(items)) fails: the new value is the same object, so React sees "no change". Always give React a new object or array. See Objects and Arrays in State.

Batching: many updates, one render

You often update several pieces of state in one go. React does not render after each call. It batches: it processes your whole event handler first, queues the updates, and then renders once with all of them applied. Since React 18 this also holds in timers, promises and any other callback, not just React events.

Batched updates

Click each button once and read the console. Batched in a handler logs handler finished (nothing rendered yet) first and then a single render: first = 11, second = 1: three updates, one render, and the third call stacked on the first (+1, then +10). Batched after await logs async handler finished and then one render: first = 12, second = 2. With flushSync logs render: first = 13, second = 2 immediately (before the handler continues), then after flushSync the DOM is already updated, then a second render first = 13, second = 3 for the update outside flushSync.

Important points:

  • A state variable is a snapshot: inside one handler, first still holds the value from the render that created the handler, even after you called setFirst. That is why the example uses functional updates (value => value + 1) to build on the queued result. See State Updates.
  • flushSync forces React to render immediately and is for rare cases such as measuring the DOM right after an update. Using it often undoes batching and costs performance.

What else can cause a render

Besides state in the component itself, a render can come from:

  • A parent rendering (the cascade rule, and the most common reason).
  • Context: when a provider's value changes (compared by Object.is), every component reading that context via useContext or use re-renders, even if memo wraps it. Components between the provider and the consumers do not have to render, as long as React receives the same children elements. Context is covered in Context.
  • An external store read with useSyncExternalStore, when its snapshot changes.
  • Hooks you call: any custom hook that uses state, context or a store brings those triggers into your component.

Things that do not cause renders: changing a ref (ref.current = x), mutating a variable, or assigning to state without calling the setter.

Reading a "why did it render" question

When you need to explain a render, ask in this order:

  1. Did this component's own state or a hook's state change?
  2. Did its parent render (and the component is not memoized, or its props changed)?
  3. Did a context it reads change value?
  4. Did a store it subscribes to publish a new snapshot?

If none apply, it did not render. The React DevTools Profiler can record "why did this render" for each component, and Profiling shows how to use it.

Practical advice

  • Do not fight renders by default. They are cheap when the output is small and unchanged. Measure first.
  • Move state down. If only one small widget needs fast-changing state, put the state inside that widget so the rest of the page never re-renders. This is often better than memo.
  • Lift content up with children. A wrapper that holds state and receives children from above does not re-render its children when its own state changes, because those elements were created by someone else.
  • Keep render pure and cheap. Move heavy calculations into useMemo only when profiling proves it, and move side effects into handlers or effects.
  • Do not count on exact render counts across React versions or between development and production. They are a debugging aid, not an API.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What are the three steps React follows to put a change on screen?

    A render is triggered, React calls components to compute the new UI (render), then applies the differences to the DOM (commit).
  2. 2. A parent component re-renders. What happens to a child that receives the same props and is not wrapped in memo?

    React renders children by default; memo is the opt-out. State is kept because the child stays at the same position in the tree.
  3. 3. A component renders and returns exactly the same JSX as last time. What does the commit phase do to the DOM?

    React compares the new output with the previous output and touches only what differs. Identical output means no DOM mutations.
  4. 4. Inside one click handler you call setCount(c => c + 1) three times. How many renders does React perform for that click?

    Updates are queued and processed together after the handler, so a single render shows the final result (here, count increased by 3).
  5. 5. Why does StrictMode render components twice in development?

    The extra render is thrown away and exists only to expose side effects in the render phase. It never happens in production.

Summary

  • React shows changes in three steps: trigger (initial render or a state update), render (call components to compute the new UI) and commit (apply the differences to the DOM, then run refs, layout effects and effects).
  • Rendering is a pure calculation that can run several times; it does not mean the DOM changed. If the output is identical, the commit makes no DOM mutations.
  • When a component renders, all its children render too unless wrapped in memo; state is kept as long as a component stays at the same place in the tree.
  • Same-value updates normally bail out; mutating state in place is invisible to React.
  • Updates are batched across handlers, timers and promises; use functional updates to build on queued values, and flushSync only in rare cases.
  • Context value changes and external store changes also trigger renders for their readers.
  • StrictMode double-renders in development to expose impure code. Treat exact render counts as debugging hints, not guarantees.

Next: memo, Referential Equality and Avoiding Re-renders