Skip to content
WANCODE
BM

Fix the Stale State

15 minIntermediateReact

A kuih shop counts box orders. Three bugs have been reported, all with the same root cause:

  1. +3 family pack only adds 1.
  2. Clicking +1 online twice quickly only adds 1 after half a second.
  3. Click +1 online, then +3 family pack straight away: the count shows 3, then drops to 1 when the online order arrives.

Expected: every box is counted. In case 3 the final count is 4.

Constraints

  • Keep the ids count, add-one, add-three, add-later, and keep the 500 ms delay for online orders.
  • Keep using useState — the fix is in how you call the setter, not in adding more state.

Fix the order counter so +3 family pack really adds 3, and online orders that arrive after a delay are never lost — even when other buttons are clicked in the meantime. Use functional state updates.

Checks

  • +3 family pack adds 3
  • +1 box still adds 1
  • Two quick online orders both arrive
  • Online order + family pack = 4 more
Preview

Console output from your code appears here.

Step 1 of 1