This project is a basic e-commerce application built using React, TypeScript, and the Context API. It includes features like product listing with pagination, adding items to the cart, and displaying the cart’s contents with quantities and total amount (summary).
-
Clone the repository:
git clone https://github.com/your-username/ecommerce-app.git
-
Navigate to the project directory:
cd ecommerce-app
-
Install the dependencies:
npm install
-
Start the development server:
npm start
src/
│
├── components/
│ ├── ProductList.tsx # Displays the list of products with pagination
│ └── Cart.tsx # Displays the items in the cart with quantities
│
├── context/
│ ├── ProductContext.tsx # Manages the product data and pagination state
│ └── CartContext.tsx # Manages the cart state and actions
│
├── types/
│ └── index.ts # Defines TypeScript types for the application
│
├── App.tsx # Main component that combines everything
└── index.tsx # Entry point of the React application
- Location:
src/context/ProductContext.tsx
- Purpose: Manages the list of products and pagination logic. Provides state like
products
,currentPage
,totalPages
, and the methodsetCurrentPage
to navigate between pages. - Usage: Wraps the main App component to provide product data and pagination controls throughout the app.
- Location:
src/context/CartContext.tsx
- Purpose: Manages the cart, including adding, removing, and clearing items from the cart. Provides state like
cartItems
and methods likeaddToCart
,removeFromCart
, andclearCart
. - Usage: Wraps the main App component to manage cart state globally.
- Location:
src/components/ProductList.tsx
- Purpose: Displays a list of products with pagination. Allows users to add products to the cart. Shows the quantity of each product added to the cart.
- Usage: Consumes
ProductContext
for product data andCartContext
for cart actions.
- Location:
src/components/Cart.tsx
- Purpose: Displays the list of items currently in the cart along with their quantities. Allows users to remove items or clear the entire cart.
- Usage: Consumes
CartContext
to display and manage cart items.
- Location:
src/App.tsx
- Purpose: The main component that combines the
ProductList
andCart
components. Wrapped byProductProvider
andCartProvider
to supply context data. - Usage: This is the entry point of the application where all components are assembled.
Once you start the development server with npm start
, you should be able to view the application in your browser at http://localhost:3000.
- The Product List will display the products with pagination controls.
- The Add to Cart button allows users to add items to their cart, and a quantity counter will show up next to the button.
- The Cart component will display the items in the cart along with options to remove items or clear the cart.
This is a simple e-commerce application built with React, TypeScript, and the Context API. It demonstrates how to manage state globally without the need for external libraries like Redux or Recoil.