Pressing 'Add' on a product in the list adds an item to the cart and updates the total. Adding the same product again increases its quantity by 1. Complete CartPage.
Build a React component that handles a product list and a cart together. Pressing 'Add' adds an item to the cart, adding the same product again increases its quantity by 1, and the total updates alongside. It all happens inside a single component with on-screen state — no server or global state.
A cart is a screen where items, quantities, and the total all have to stay in agreement as the user acts. Because several values are linked, the key is keeping the related values moving consistently whenever one of them changes. In React you hold the on-screen values in state and update that state on each action so the view re-renders. What makes the behavior trustworthy is verifying not just a single action but repeated ones — like adding the same product several times — and checking that the quantity and total still agree.