
A modern, fully functional React Shopping Cart application designed for learning React fundamentals. This project demonstrates how to manage state with useReducer and Context API, fetch data asynchronously, and implement real-world cart features using efficient JavaScript data structures like Map.
- Live-Demo: https://cart-arnob.netlify.app/
- Project Summary
- Features
- Technology Stack
- Project Structure
- Installation & Running
- Application Walkthrough
- Global State & useReducer
- API & Data Management
- Component Overview
- Key Code Examples
- Learning Purposes & Keywords
- Conclusion
This project is a simple shopping cart application built with React, using the useReducer
hook for robust state management and the Context API for sharing global state. The cart is implemented as a JavaScript Map, optimizing for fast lookups and updates. The project is ideal for learners seeking to understand intermediate React concepts and modern JavaScript patterns used in e-commerce apps.
- Add, remove, and update items in the cart
- Adjust item quantities (increase/decrease)
- Clear the entire cart
- Fetch initial cart data from an external API
- Calculate and display cart totals (subtotal, total items)
- Efficient cart state management using Map
- Modern React patterns: useReducer, Context API, custom hooks
- Responsive and simple UI
- React (with hooks)
- JavaScript (ES6+)
- Vite (for development/build)
- Context API & useReducer
- HTML/CSS
- External API (for cart data)
Cart--React-Fundamental-Project-14/
├── .gitignore
├── Cart.fig
├── README.md
├── index.html
├── package.json
├── package-lock.json
├── vite.config.js
├── public/
└── src/
├── App.jsx
├── main.jsx
├── components/
│ ├── CartContainer.jsx
│ ├── CartItem.jsx
│ └── ...other components
├── context/
│ ├── cartContext.js
│ ├── reducer.js
│ └── actions.js
├── data/
│ └── ... (for static/fetched data if needed)
└── styles/
└── ... (CSS/SCSS files)
*Note: Actual filenames may vary. Explore
/src
for up-to-date code structure.
1. Clone the Repository
git clone https://github.com/arnobt78/Cart--React-Fundamental-Project-14.git
cd Cart--React-Fundamental-Project-14
2. Install Dependencies
npm install
3. Start Development Server
npm run dev
Visit http://localhost:5173
(or the port shown in your terminal) in your browser.
Open the app to browse a list of items fetched from the API, with add/remove and quantity controls for each item.
- Add to Cart: Click to add items to the cart (or adjust quantities).
- Remove Item: Remove a specific item from the cart.
- Increase/Decrease Amount: Buttons to adjust quantity per item.
- Clear Cart: Remove all items from the cart with one action.
- Total Calculation: Cart subtotal and item count update automatically.
- Initial Data Load: Cart data is fetched from:
const url = "https://www.course-api.com/react-useReducer-cart-project";
The cart state is managed globally using React Context and the useReducer hook.
- Context API: Provides cart state/functions to all components.
- useReducer: Handles cart logic via action types (e.g., CLEAR_CART, REMOVE_ITEM, INCREASE, DECREASE, FETCH_DATA, CALCULATE_TOTALS).
Reducer Example:
function cartReducer(state, action) {
switch(action.type) {
case 'CLEAR_CART':
return { ...state, cart: new Map() };
// Other cases: REMOVE_ITEM, INCREASE, DECREASE, etc.
default:
return state;
}
}
- Fetching Data: Uses
fetch
oraxios
to pull cart items from an external API. - Cart State Shape: Uses
Map
for efficient item access by ID.
Example:
const cart = new Map();
// Add item
cart.set("itemId", { id: "itemId", name: "Item", price: 10, quantity: 1 });
// Remove item
cart.delete("itemId");
- App.jsx: Root component, sets up context providers and main layout.
- CartContainer.jsx: Renders the entire cart, totals, and clear button.
- CartItem.jsx: Displays individual cart item, with controls for quantity and remove.
- Context/Reducer.js: Contains logic for updating cart state.
const cart = new Map();
cart.set("apple", { name: "Apple", price: 0.5, quantity: 3 });
cart.get("apple"); // { name: "Apple", price: 0.5, quantity: 3 }
cart.delete("apple");
for (let [key, value] of cart) {
console.log(key, value);
}
const items = [
{ id: 1, name: "first", price: 10 },
{ id: 2, name: "second", price: 20 },
];
const cart = new Map(items.map((item) => [item.id, item]));
dispatch({ type: 'INCREASE', payload: { id: 'itemId' } });
- React Hooks (
useReducer
,useContext
) - State management patterns
- Efficient data structures (
Map
) - API data fetching
- Component composition
- E-commerce cart logic
- Modern JavaScript (ES6+)
- Context API
This project is a practical guide to mastering React’s state management and component patterns, with a focus on real-world e-commerce features. It’s built for both learning and demonstration, suitable for teaching how to architect robust React apps with modern best practices.
For further exploration, try extending the project with features like user authentication, product search, sorting, or integrating a backend.
Happy Coding!