Course contentsShow
← React
Lesson 347 of 2,39610. useState BasicsFree lesson

Multiple setState Calls in One Event

See how React batches multiple setState calls from the same event handler into one re-render.

Multiple setState Calls in One Event

You'll learn: How React cleverly batches multiple state updates from the same event handler into a single re-render for better performance.

What Happens with Multiple Calls?

When you call multiple state setters in the same event handler, you might expect multiple re-renders. Surprisingly, React is smarter than that! It batches all the state updates together and performs just one re-render at the end.

Think of it like a shopping list: instead of making a separate trip to the store for each item (expensive!), you write down everything you need, then make one efficient trip.

How It Works

function MultiUpdateExample() {
  const [count, setCount] = useState(0);
  const [name, setName] = useState('');
  const [isActive, setIsActive] = useState(false);

  function handleClick() {
    setCount(1);
    setName('Alice');
    setIsActive(true);
    // React batches all three updates → one re-render!
  }

  return (
    <div>
      <p>Count: {count}</p>
      <p>Name: {name}</p>
      <p>Active: {isActive ? 'Yes' : 'No'}</p>
      <button onClick={handleClick}>Update All</button>
    </div>
  );
}

When you click the button, React doesn't render three times. It collects all three state changes, then renders once with all the new values.

Why This Matters

Without batching, each setState would trigger a separate re-render, making your app slower and potentially showing inconsistent "in-between" states to the user. Batching keeps your UI fast and your updates atomic.

Key Takeaway: React automatically batches multiple setState calls from the same event handler into a single re-render, improving performance without any extra work from you.