Batching Hides Individual Updates
What you'll learn: Why multiple setState calls in one event handler seem to "disappear" into a single re-render.
The Mystery of Missing Renders
You've learned that React batches multiple state updates into one re-render for performance. But here's the interesting consequence: you never actually see the intermediate states on screen.
Imagine you write this in a click handler:
setCount(count + 1);
setName('Alice');
setActive(true);
You might expect three separate renders—first with the new count, then with the new name, then with active true. But React batches them, so only one render occurs with all three changes applied together.
Why This Matters
This batching behavior means:
- The DOM updates once, not three times
- Users never see partial updates—it's all-or-nothing
- console.log(count) right after
setCountwon't show the new value (remember: state updates are asynchronous) - Effects and refs only see the final, batched result
Think of it like editing a document. Instead of auto-saving after every keystroke (slow!), React waits until you pause, then saves all your changes at once. The intermediate edits never get committed.
The Hidden Benefit
Batching doesn't just improve performance—it prevents visual glitches. If you update both a loading flag and data simultaneously, the user never sees a flash where one updated but the other didn't yet.
Key Takeaway: React's batching makes multiple setState calls behave like a single atomic update—the component only re-renders once with all changes applied, and intermediate states are never visible to the user or the DOM.