React 18 Alpha: Automatic Batching

React 18’s best feature might be the one you don’t have to code for: Automatic Batching.

function handleClick() {
  fetchSomething().then(() => {
    // React 17: Causes TWO re-renders (because it's inside a Promise)
    setCount(c => c + 1);
    setFlag(f => !f);
    
    // React 18: AUTOMATICALLY causes ONE re-render
  });
}

Previously, React only batched updates inside event handlers. Now, updates inside Promises, timeouts, and native event handlers are all batched. This is a free performance upgrade.


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.