Sub topic 3
How a state update triggers a re-render
This chapter assumes you know function components, JSX, props, and arrow functions. It also assumes you have seen useState declared, as in the previous chapter.
Rendering means calling your component
In React, rendering means React calls your component function and uses the JSX it returns. A re-render is another call to the same function. Each call gets its own local variables, so nothing local survives between calls. React stores state outside the function and hands the current value back to useState on each call.
The update cycle
Calling a setter does not change the variable you already have. It asks React to render again with a new value. The cycle has four steps:
- An event, such as a click, runs your handler.
- The handler calls a setter, such as
setCount(1). React records the new value and schedules a render. - React calls the component again.
useStatenow returns the new value. - React compares the new JSX with the previous JSX and updates the DOM only where they differ.
Watching it happen
This component logs on every render, so you can see when the function is called.
import { useState } from 'react';
function Counter() {
console.log('render');
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
console.log('in handler:', count);
}
return <button onClick={handleClick}>Clicked {count} times</button>;
}On the first click, the console shows in handler: 0 and then render. The handler still sees 0 because it belongs to the render that created it. The new value, 1, only appears in the next call of the component. The next chapters build on this behavior.
When React skips a render
If you set the same value the state already holds, React compares the old and new values with Object.is. When they are equal, React usually bails out and does not re-render the children or touch the DOM. React may still call your component once more before it bails out, so keep render logic free of side effects.
Objects and arrays are compared by reference. setPerson(person) after mutating person passes the same reference. React sees no change, and the screen stays stale. A new object, such as { ...person, name: 'Ada' }, has a new reference and triggers a render.
Re-rendering children
When a component re-renders, React also re-renders the components it returns, even if their props did not change. This is normally fast. Leave it alone until you measure a real problem.
Common errors
- Expecting a variable to change right after the setter call. It keeps its value for the rest of that render.
- Changing a variable with
letand expecting the screen to update. React never learns about it, so no render is scheduled. - Mutating an object or array and passing it back to the setter. The reference is the same, so React skips the update.
Exercise
A handler calls setCount(count + 1) and then logs count. The state was 5 before the click. What does the log print, and what does the component show after the render?
Show answer
The log prints 5, because the handler sees the count from the render that created it. After React re-renders, the component shows 6.