State Batch Update
Predict Output Medium
Question
What does this code print?
Code
function App() {
const [a, setA] = useState(0);
const [b, setB] = useState(0);
const click = () => { setA(1); setB(1); };
// How many re-renders from one click?
Hint
React batches state updates in event handlers.
Explanation
In React 18, state updates in event handlers are automatically batched. setA(1) and setB(1) are batched into a single re-render, not two. This improves performance. (In React 17, batching only applied to React event handlers, not setTimeout/promise; React 18 batches everywhere.)
Related Resources
Related Lessons
- State
Learn state.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.