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.
Click Add Item — Count shows 1, Total shows 0. Click again — Count shows 2, Total shows 10. Total is always one step behind.
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.
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.