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

State Updates Are Asynchronous

Learn why setState doesn't update state immediately—React batches updates for performance.

State Updates Are Asynchronous

What you'll learn: Why calling setState doesn't change your state variable immediately in the next line of code.

The Surprise

When you call a state setter function like setCount(count + 1), you might expect count to have the new value immediately. It doesn't. React batches state updates and processes them asynchronously for performance reasons.

function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
    console.log(count); // Still 0! Not 1.
  }

  return <button onClick={handleClick}>Count: {count}</button>;
}

After calling setCount(count + 1), if you log count on the next line, you'll see the old value. The state variable hasn't updated yet.

Why React Does This

React collects multiple setState calls happening in the same event (like a click) and processes them together in a single re-render. This is called batching. It's a huge performance win—instead of re-rendering three times for three separate setState calls, React re-renders once.

Think of it like ordering at a restaurant: instead of the waiter running to the kitchen after every single item you mention, they write down your whole order and make one trip.

What Actually Happens

When you call setCount, you're scheduling a state update. React will:

  1. Queue the update
  2. Finish running your current code
  3. Re-render your component with the new state value

The new value appears in the next render, not in your current function execution.

Key Takeaway: State updates are asynchronous and batched—setState schedules an update for the next render, it doesn't mutate your state variable immediately.