Shopping Cart — Manage Complex State with useReducer

Hooks & State — Intermediate React challenge.

Manage a shopping cart's state with useReducer instead of useState. Multiple related actions — adding, adjusting quantity, removing, clearing — each need different update logic, which is exactly the kind of complexity useReducer is built for.

Requirements

Signature

function cartReducer(state: { items: CartItem[] }, action: CartAction): { items: CartItem[] }
function Cart(): JSX.Element

Example

cartReducer({ items: [] }, { type: "ADD_ITEM", payload: { id: 1, name: "Keyboard", price: 50 } })
// ⇒ { items: [{ id: 1, name: "Keyboard", price: 50, quantity: 1 }] }

Constraints

🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code
Loading workspace…