Description

A cart shows item count and total price. Adding an item should increment count by 1 and add 10 to the total in the same click. The total always shows 10 less than expected.

Anomaly

Click Add Item — Count shows 1, Total shows 0. Click again — Count shows 2, Total shows 10. Total is always one step behind.

Constraint
Fix must use functional updater form for both setCount and setTotal
Do not merge them into a single state object
Hint
Consult the SenseiOnly for those truly stuck · Flip to reveal

When two setState calls both read from the same stale variable in the same synchronous block, the second one sees the pre-update value. Use the functional updater form for both.

Loading editor…
Correct Solution
Loading...
Expected OutputGoal State
Your OutputLive