CodePark/ questions / State
START PRACTICINGPRACTICE
Frontend question · React › State

number is 0. What is it after one click?

<button onClick={() => {
  setNumber(number + 5);
  setNumber(n => n + 1);
}}>Increase</button>
6

The answer is 6. React queues both updates and applies them in order. The first call replaces the value with 0 + 5. The updater then receives that pending value, 5, and returns 5 + 1.

1 is the tempting wrong answer. It assumes the updater sees the old snapshot, 0. But updaters always receive the latest queued state.