Course contentsShow
← React
Lesson 417 of 2,39612. Rendering and BatchingFree lesson

What is Batching and Why It Matters

Understand how React groups multiple state updates into a single re-render to improve performance.

What is Batching and Why It Matters

What you'll learn: How React groups multiple state updates together to avoid unnecessary re-renders and keep your app fast.


The Core Idea

Imagine you're updating a shopping list app and the user clicks a button that changes three pieces of state at once: the item count, the total price, and a "saved" flag. Without batching, React would re-render your component three separate times—once for each state update. That's wasteful!

Batching is React's optimization where multiple state updates that happen close together are grouped into a single re-render. Instead of three renders, you get one.

How It Works

When you call multiple setState functions in the same event handler, React doesn't re-render immediately after each one. Instead, it collects all the updates, processes them together, and then performs just one render with all the new state values:

function handleClick() {
  setCount(count + 1);      // Queued
  setPrice(price + 10);     // Queued
  setSaved(true);           // Queued
  // Only ONE re-render happens after this function completes
}

Why It Matters

Performance: Re-rendering is usually fast, but it's not free. Batching prevents unnecessary work—React only recalculates your component tree and updates the DOM once instead of multiple times.

Consistency: All state updates appear together in the next render, preventing intermediate "half-updated" UI states that users might briefly see.

The Mental Model

Think of batching like collecting emails to send in one trip to the mailbox instead of walking there separately for each letter. Same result, much more efficient.


Key Takeaway: React automatically batches multiple state updates in event handlers into a single re-render, improving performance and keeping your UI consistent.