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

React 18's Automatic Batching Everywhere

Discover how React 18 extends batching to timeouts, promises, and native events automatically.

React 18's Automatic Batching Everywhere

What you'll learn: How React 18 automatically batches state updates in timeouts, promises, and native event listeners—not just synthetic events.

The Old Limitation

Before React 18, automatic batching only worked inside React event handlers like onClick or onChange. If you called setState multiple times inside a setTimeout, a promise's .then(), or a native browser event listener (like addEventListener), React would re-render after each update—not batched.

// React 17 and earlier
setTimeout(() => {
  setCount(c => c + 1);  // Causes re-render
  setFlag(true);         // Causes another re-render
}, 1000);

This meant two renders for two updates, even though they happened in the same function.

React 18's Upgrade

React 18 extends automatic batching everywhere. Whether your state updates happen in a timeout, a fetch response, or a native event listener, React now batches them automatically:

// React 18
setTimeout(() => {
  setCount(c => c + 1);  
  setFlag(true);         
  // Only ONE re-render for both updates!
}, 1000);

fetch('/api').then(() => {
  setData(result);
  setLoading(false);
  // Batched into one render
});

The Analogy

Think of batching like collecting mail. Before React 18, the mail carrier (React) only grouped deliveries when you handed letters directly (event handlers). But if you put letters in a timed delivery box (setTimeout) or sent them via courier (promises), they were delivered separately. Now the carrier groups all deliveries—no matter how they're scheduled.

Key Takeaway: React 18 automatically batches all state updates regardless of where they occur—timeouts, promises, native events—eliminating unnecessary re-renders without any code changes.