Exercise: Shopping Cart with useReducer
Objective
Build a complete shopping cart using useReducer for state management.
Requirements
State Structure
interface CartState {
items: CartItem[];
total: number;
discount: number;
tax: number;
}
interface CartItem {
id: string;
name: string;
price: number;
quantity: number;
image?: string;
}
Actions
- ADD_ITEM
- REMOVE_ITEM
- UPDATE_QUANTITY
- APPLY_DISCOUNT
- CLEAR_CART
Features
- Add products to cart
- Update quantities (+ / -)
- Remove items
- Calculate subtotal, tax, and total
- Apply discount codes
- Persist to localStorage
- Empty cart state
Bonus
- Multiple discount codes
- Free shipping threshold
- Product recommendations
- Checkout validation
Time Estimate
3-4 hours