Intermediate
Fix the cart total by deriving it
This cart stores total and count in state next to the items. Adding works, but removing an item leaves the total and the count wrong.
Fix it by deleting the total and count state and calculating both from items during render.
Keep the ids: #count shows N items and #total shows Total: ₹N. The button #add adds a Chai for ₹30, and every row has a .remove button.
What Check tests
- It starts at
2 itemsandTotal: ₹35 - Adding a Chai gives
3 itemsandTotal: ₹65 - Removing Tea gives
2 itemsandTotal: ₹45 - Removing everything gives
0 itemsandTotal: ₹0 totalandcountare no longer state
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Your page
Press Run to see the page.
Console
Nothing logged yet.