Three setters in one click lead to one render, not three. Watch the queue fill up and empty in a single pass.
React · intermediate · 6 min · 16 steps · free
import { useState } from 'react';
export default function Signup() {
const [name, setName] = useState('');
const [age, setAge] = useState(0);
const [saved, setSaved] = useState(false);
function fillIn() {
setName('Ada');
setAge(36);
setSaved(true);
}
return (
<div>
<p>{saved ? `Saved ${name}, ${age}` : 'Not saved yet'}</p>
<button onClick={fillIn}>Fill in and save</button>
</div>
);
}<Signup> for the first time: it calls the Signup function.useState runs for the first time, so React stores the starting value for name and returns it: "".useState runs for the first time, so React stores the starting value for age and returns it: 0.useState runs for the first time, so React stores the starting value for saved and returns it: false.Signup returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.onClick handler.useState returns the value React has kept for name: "Ada". The starting value is only used on the first render.useState returns the value React has kept for age: 36. The starting value is only used on the first render.useState returns the value React has kept for saved: true. The starting value is only used on the first render.Signup returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.Answer: One. Automatic batching.
Answer: After the handler finishes
← When Does a Component Render? · Stale Closures →