Skip to content
YouTurkey Tech
All React topics

What happens when state changes

Intermediate

From setState to the screen: which components render again and which do not, how React compares the output, and why only one text node in the DOM changes.Builds on Closures

Tree

Empty

Console

Empty

0/13Press Play, or step through with the arrows, to begin.
Step 0 of 13

Explanation

3 min read

On this page

What happens in React when state changes?

Calling a state setter like setCount does not change anything on the spot. It queues an update and asks React to render again. React then works in two phases:

  • Render. React calls your components to find out what the screen should look like now. A component is a function: it receives props and state and returns JSX, a description of the UI. Nothing on the page changes during this phase.
  • Commit. React compares the new description with the previous one and applies only the differences to the DOM: a text that changed, an attribute, an element added or removed. Elements that are the same are left exactly as they are.

So a "re-render" means "React called your function again", not "the page was redrawn". Many renders end with a tiny commit, or none at all.

Which components re-render, and why doesn't the sibling?

When a component's state changes, React re-renders that component and everything it renders, down the tree. In the lesson, Counter owns the state, so Counter and its child Label run again.

App and Footer do not. App is above Counter and its own props and state did not change; Footer is beside Counter, not inside it. React does not need to call either of them to know their output is the same.

Children re-render even when their props did not change, simply because their parent ran again. That is usually fast. When a child is expensive, wrapping it in memo lets React skip it while its props stay equal.

Why is state a snapshot?

Each render has its own props and state, fixed for that render. The event handlers created during a render see that render's values, like a photo taken at that moment. When the button's handler runs setCount(count + 1), count is the value from the render that created the handler, 0, even after the update is queued. The new value only appears in the next render, when useState returns it.

What mistakes do people make with React state?

Mini example

Pressing the button once shows 1, not 3: in this render count is 0 on all three lines. The updater form fixes it.

function Counter() {  const [count, setCount] = useState(0);  function addThree() {    setCount(count + 1);    setCount(count + 1);    setCount(count + 1); // count is still 0 here: the result is 1    // setCount((c) => c + 1) three times gives 3  }  return <button onClick={addThree}>{count}</button>;}

Quick check

The button in the mini example is pressed once. What does it show?

Further reading

All steps as text
  1. Step 1:

    First render. React calls App, the root component. It returns a main element with Counter and Footer inside.

    Lines 1–8

  2. Step 2:

    React calls Counter. useState(0) gives it its state, count = 0, and it returns a button with a Label.

    Lines 10–17

  3. Step 3:

    Label is called with the prop count={0}. It returns a b element: the number, then the text " clicks".

    Lines 14–19

  4. Step 4:

    Footer renders too. It takes no props and returns a footer.

    Line 20

  5. Step 5:

    That was the render phase: React only called functions. Now it commits, creating the DOM nodes and putting them on the page.

  6. Step 6:

    You click the button. Its handler calls setCount(count + 1), and count is 0 in this render, so it asks for 1.

    Line 13

  7. Step 7:

    setCount doesn’t change count right away. It queues an update and tells React that Counter must render again.

    Line 13

  8. Step 8:

    React re-renders Counter: it calls the function again, and this time useState returns 1.

    Lines 10–11

  9. Step 9:

    Counter’s new output has Label with count={1}, so Label re-renders too. A component re-renders with its parent.

    Lines 14–19

  10. Step 10:

    App and Footer don’t run again. The state belongs to Counter, so React starts there and only goes down.

    Lines 4–5

  11. Step 11:

    React compares the new output with the old one. Same button, same b, same " clicks"; only the number changed.

    Line 19

  12. Step 12:

    Commit: React sets that one text node to 1. Nothing else in the DOM is touched.

  13. Step 13:

    Render calls the components below the state that changed. Commit applies only the difference to the DOM.