Pre-React 18 Batching Limitations
You'll learn why React 17 and earlier versions sometimes caused "extra" re-renders that React 18 now avoids.
The Old Batching Boundary
Before React 18, batching (grouping multiple state updates into one render) only worked inside React event handlers like onClick, onChange, etc. As soon as your code left React's control—inside a setTimeout, fetch().then(), or Promise callback—batching stopped working.
Why setTimeout Broke Batching
// React 17 behavior:
function handleClick() {
setState1(newValue); // ✅ Batched
setState2(newValue); // ✅ Batched together
// → 1 re-render
setTimeout(() => {
setState3(newValue); // ❌ Not batched!
setState4(newValue); // ❌ Not batched!
// → 2 separate re-renders
}, 0);
}
The moment you stepped outside the event handler (even with a 0ms timeout), React no longer "knew" to batch your updates. Each setState triggered its own render cycle.
The Fetch Callback Problem
// React 17:
fetch('/api/data')
.then(response => response.json())
.then(data => {
setLoading(false); // Render #1
setData(data); // Render #2
setError(null); // Render #3
});
Three state updates = three renders. Users might briefly see inconsistent UI states flash by.
Why This Happened
React 17's batching relied on being inside its "event handler execution context." Once your callback was scheduled for later (async), React had already exited that context—no more automatic batching.
Key Takeaway: Pre-React 18, only synchronous updates inside React event handlers were batched—async callbacks like setTimeout and fetch().then() caused extra renders, which React 18's automatic batching everywhere now prevents.