Command Palette

Search for a command to run...

Hectal
Phase 6: Managing State
React · Exercise 2 of 5 PreviousNext
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

  1. It starts at 2 items and Total: ₹35
  2. Adding a Chai gives 3 items and Total: ₹65
  3. Removing Tea gives 2 items and Total: ₹45
  4. Removing everything gives 0 items and Total: ₹0
  5. total and count are 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.