A cart management system built with React's useReducer hook, focusing on advanced state management patterns and performance optimization. This project demonstrates how to handle complex state transitions in larger applications using modern React patterns.
Primary Learning Objective: Learning how to use useReducer for complex state operations in scalable applications.
Note: UI design provided by course instructor - implementation focused exclusively on state management logic and architecture. View Figma Design
View Live on Netlify:
React useReducer Cart Demo
๐ Full Cart Functionality |
![]() |
๐ฑ Responsive Design |
![]() |
Fully functional cart with responsive design and real-time calculations |
- Map Data Structure: Replaced traditional arrays with Map for O(1) lookups and updates
- Key-Based Operations: Efficient item management using unique IDs as keys
- Immutability Patterns: Proper state updates with new Map instances
// Action โ Reducer โ State Update Pattern
dispatch({ type: 'ACTION', payload: data })
โ reducer(state, action)
โ new State โ UI Update
- ๐ useReducer State Management-Complex state transitions with predictable updates
- ๐บ๏ธ Map Data Structure-High-performance cart operations with O(1) complexity
- ๐ API Integration-Dynamic data fetching with loading states
- ๐ฑ Responsive Design -Seamless transition between mobile and desktop views
- ๐งฎ Real-time Calculations - Automatic total amount and cost updates
- ๐ฏ Context API - Global state management across components
Tool / Library | Purpose |
---|---|
โก Vite | Fast build tool & dev server |
โ๏ธ React 19 | Component-based UI |
๐ useReducer | Advanced state management |
๐ฏ Context API | Global state provider |
๐บ๏ธ Map API | High-performance data structure |
๐ React Icons | Icon library for menu items |
๐ Fetch API | Data fetching from external API |
- useReducer Implementation- Handling complex state logic beyond useState
- Context API Integration- Global state management across components
- Custom Hook Creation - Reusable state logic with
useGlobalContext
- Map vs Array-O(1) operations vs O(n) for frequent updates
- Efficient Rendering-Components update only when relevant state changes
- Memoized Calculations-Totals computed from current state
- Map Benefits- Faster lookups, built-in iteration methods, key-based access
- When to Use Map- Frequent updates, large datasets, key-based operations
- Conversion Patterns -
Array.from(map.entries())
for rendering
src/
โโโ components/ # UI Components
โ โโโ CartContainer.jsx # Main cart layout
โ โโโ CartItem.jsx # Individual cart item
โ โโโ Navbar.jsx # Navigation with cart count
โโโ contexts/ # State Management
โ โโโ AppContext.js # React context creation
โ โโโ AppProvider.jsx # Global state provider
โ โโโ reducer.js # Core state logic
โ โโโ actions.js # Action definitions
โโโ hooks/ # Custom Hooks
โ โโโ useGlobalContext.js # Context consumption
โโโ utils/ # Business Logic
โ โโโ getTotals.js # Cart calculations
โโโ data.js # Initial mock data
const reducer = (state, action) => {
if (action.type === "INCREASE") {
const newCart = new Map(state.cart);
const item = newCart.get(action.payload.id);
const newItem = { ...item, amount: item.amount + 1 };
newCart.set(action.payload.id, newItem);
return { ...state, cart: newCart };
}
// ... other actions
};
Operation | Array | Map |
---|---|---|
Find item | O(n) | O(1) |
Update Item | O(n) | O(1) |
Delete Item | O(n) | O(1) |
Clear All | O(1) | O(1) |
- Add/Remove Items - Efficient Map operations
- Quantity Management - Increase/decrease with boundary checks
- Total Calculations- Real-time amount and cost updates
- API Integration - Dynamic data loading with state management
- Node.js โฅ 18.0.0
- npm or yarn package manager
- Clone the repository
git clone https://github.com/pro804/React-Cart-App.git
- Navigate to the project directory
cd React-Cart-App
- Install dependencies
npm install
- Start the development server
npm run dev
- Open http://localhost:5173 to view it in the browser.
npm run dev
โ Runs the development server (Vite)npm run build
โ Builds the app for productionnpm run preview
โ Previews the production build locally
This project was created for educational purposes as part of a React learning journey. This project is licensed under the MIT License. See the LICENSE file for details.