Skip to content

A React cart application demonstrating advanced state management with useReducer and Map data structure. Focus on complex state transitions and performance optimization.

License

Notifications You must be signed in to change notification settings

pro804/React-Cart-App

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

12 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

React useReducer Cart ๐Ÿ›’โšก

React Vite Netlify JavaScript License: MIT

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.

๐ŸŽฏ Project Focus: State Management Logic

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

๐Ÿš€ Live Demo

View Live on Netlify:
React useReducer Cart Demo

๐Ÿ“ธ Project Preview

๐Ÿ›’ Full Cart Functionality
Cart functionality showing add, remove, and quantity controls
๐Ÿ“ฑ Responsive Design
Mobile responsive cart interface
Fully functional cart with responsive design and real-time calculations

๐Ÿ—๏ธ Architecture Highlights

Advanced Data Structure Implementation

  • 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

State Management Pattern

// Action โ†’ Reducer โ†’ State Update Pattern
dispatch({ type: 'ACTION', payload: data })
  โ†’ reducer(state, action)
  โ†’ new State โ†’ UI Update

โœจ Core Features

  • ๐Ÿ”„ 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

๐Ÿ› ๏ธ Built With

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

๐ŸŽ“ Key Learning Outcomes

Advanced State Management

  • useReducer Implementation- Handling complex state logic beyond useState
  • Context API Integration- Global state management across components
  • Custom Hook Creation - Reusable state logic with useGlobalContext

Performance Optimization

  • 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

Data Structure Decisions

  • 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

๐Ÿ—๏ธ Project Structure

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

๐Ÿ”ง Technical Implementation

Reducer Pattern

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
};

Performance Comparison

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)

Cart Operations

  • 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

๐Ÿš€ Getting Started

Prerequisites

  • Node.js โ‰ฅ 18.0.0
  • npm or yarn package manager

Installation

  1. Clone the repository
git clone https://github.com/pro804/React-Cart-App.git
  1. Navigate to the project directory
cd React-Cart-App
  1. Install dependencies
npm install
  1. Start the development server
npm run dev
  1. Open http://localhost:5173 to view it in the browser.

๐Ÿ”ง Available Scripts

  • npm run dev โ€” Runs the development server (Vite)
  • npm run build โ€” Builds the app for production
  • npm run preview โ€” Previews the production build locally

๐Ÿ“„ License

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.

About

A React cart application demonstrating advanced state management with useReducer and Map data structure. Focus on complex state transitions and performance optimization.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published