Sub topic 3
What triggers a re-render
This chapter assumes you know JavaScript functions, objects, and arrays, basic React components, props, and JSX, and the useState hook. It explains what makes React call your component function again.
Rendering means calling your component
In React, "rendering" means React calls your component function to get a description of the UI. It does not mean the browser repaints. A re-render is another call to that function, made because something changed. Whether the DOM changes afterward is a separate question, covered in later chapters.
The trigger: a state update
A component re-renders when its own state is updated through a setter such as setCount. The setter does not change the variable right away. It tells React to schedule a new render, and React calls the component with the new value.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
console.log('Counter rendered with count', count);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}The first log appears on the initial render. Each click schedules another render, so the log runs again with the new count. The log comes before any DOM update because rendering only computes the next UI.
Re-rendering spreads to children
When a component re-renders, React also re-renders every child component it returns. This happens even if the props did not change. React does not check props by default.
import { useState } from 'react';
function Child() {
console.log('Child rendered');
return <p>I am a child</p>;
}
function Parent() {
const [count, setCount] = useState(0);
return (
<>
<button onClick={() => setCount(count + 1)}>{count}</button>
<Child />
</>
);
}Every click logs "Child rendered", although Child has no props. The parent rendered, so the child rendered with it. The chapter on memoization shows how to opt out of this.
Other triggers
The sources of a re-render are few:
| Trigger | Re-renders |
|---|---|
| Initial mount | The root component and its whole subtree |
| State setter called with a new value | That component and its descendants |
| Parent re-renders | Its children, whatever their props |
| Context value changes | Every component that reads that context |
A useReducer dispatch behaves like a state setter. Changing a prop does not trigger a render by itself. A prop only changes because a parent rendered first.
What does not trigger a re-render
Two common mistakes follow from this model.
- Mutating a variable. Assigning to a plain variable, or changing a
letinside the component, does not tell React anything. The screen stays as it was. - Mutating state in place. Calling
todos.push(x)and thensetTodos(todos)passes the same array reference. React compares the new value to the old one withObject.is, sees no difference, and may skip the render. Pass a new array instead, such assetTodos([...todos, x]).
React also skips the update when you set state to the value it already holds, for example setCount(count). Sometimes React still calls the component once more before it bails out. It will not update the DOM or re-render the children.
A stale value inside one render
Each render sees fixed values. Inside one call of a component, count never changes, even after you call setCount. This matters when you update state more than once in a handler. The next chapters cover batching and the updater form setCount(c => c + 1). For now, remember that a setter schedules work and never changes the current render.
Exercise
A component calls const [user, setUser] = useState({ name: 'Ada' }). A button handler runs user.name = 'Grace'; setUser(user);. The screen does not update. Why, and how do you fix it?
Show answer
The handler mutates the existing object and passes the same reference back. React compares the old and new state with Object.is, finds them identical, and skips the re-render. Create a new object instead: setUser({ ...user, name: 'Grace' }).