# Functional State Updates

> How to safely update state multiple times in a row by using an updater function.

Subject: React | Lesson 9 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/functional-state-updates
Previous: [State & useState](https://codewithanimation.in/react/state-basics.md) | Next: [Event Handling](https://codewithanimation.in/react/event-handling.md)

## What it is

Instead of passing a new value directly to a state setter (e.g., `setCount(count + 1)`), you can pass a function: `setCount(prevCount => prevCount + 1)`.

This function is called an **updater function**. React puts these functions in a queue and runs them in order, passing the returned value of the previous function as the input to the next one.

## Why it matters

Because state is a snapshot taken at the time of render, a variable like `count` does not change during the execution of your event handler.

If `count` is 0, and you write `setCount(count + 1)` three times in a row, you are effectively writing `setCount(0 + 1)` three times. The final result will be 1, not 3.

Using an updater function ensures you are always operating on the absolute latest state, even if multiple updates are queued before the next render.

## How it works

When you call `setCount(c => c + 1)`, React adds that function to a queue.

When it's time to process the queue, React takes the current state (say, `0`), passes it to the first function (`0 => 0 + 1`), and takes the result (`1`).

It then passes that result to the next function in the queue (`1 => 1 + 1`), resulting in `2`. This ensures updates are never lost.

## Common mistake: The stale closure trap

If you use a `setTimeout` or an asynchronous `fetch` call that resolves 3 seconds from now, the `count` variable inside that function will still be the value it was when the timer started.

If you call `setCount(count + 1)` inside the timeout, you will overwrite any changes that happened during those 3 seconds. Using `setCount(prev => prev + 1)` makes the update safe against time delays.

## Tip: When to use it

Rule of thumb: If your new state depends on the old state, ALWAYS use an updater function (`setCount(prev => prev + 1)`). If the new state is completely independent of the old state, use the direct value (`setName('Alice')`).

## Try it: The +3 Bug

Try the broken button. It tries to add 3 by calling the setter three times, but it fails. The fixed button uses an updater function to successfully queue the math.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/functional-state-updates

## Check yourself

### Question 1

If `count` is 0, what is the value of `count` after these three lines execute? `setCount(count + 1); setCount(count + 1); setCount(count + 1);`

- A. 1
- B. 3
- C. 0

Correct answer: A

State is a snapshot. The `count` variable does not change until the NEXT render. So all three calls read `count` as 0.

- Why not B: Because `count` is 0 during the entire function execution, you are just calling `setCount(1)` three times.
- Why not C: The setter successfully queues a render with the value 1.

### Question 2

How do you fix the bug from the previous question so the final result is 3?

- A. Use `async/await` on the setter functions.
- B. Pass an updater function instead: `setCount(c => c + 1)`
- C. Use `this.setState` instead of `useState`.

Correct answer: B

Passing a function puts the updates in a queue, where each function receives the return value of the previous function.

- Why not A: State setters in React do not return Promises.
- Why not C: Both `useState` and `this.setState` suffer from the exact same batching behavior.

### Question 3

When is it absolutely necessary to use an updater function?

- A. Only when updating state inside a `useEffect` hook.
- B. When the new state is calculated based on the previous state.
- C. When you are updating a string or boolean instead of a number.

Correct answer: B

If your new state needs to know what the old state was, you must use an updater function to guarantee you have the latest value.

- Why not A: It's needed anytime the next state depends on the previous state, regardless of where it happens.
- Why not C: The data type does not matter. The rule applies to objects, arrays, booleans, and strings alike.

## Remember this

- State variables do not change during a render; they are static snapshots.
- Calling `setState(value)` multiple times in a row will only apply the final call.
- Pass a function (`setState(prev => prev + 1)`) to queue updates safely.
- Always use an updater function if your new state depends on the previous state.
