/Add_To_Cart-using-React-Modal

A simple and responsive ReactJS application that fetches product data from the Fake Store API, displays them in a card layout, and allows users to add or remove products from a shopping cart via a modal interface.

Primary LanguageHTML

๐Ÿ›’ Add to Cart Using React Modal

A simple and responsive ReactJS application that fetches product data from the Fake Store API, displays them in a card layout, and allows users to add or remove products from a shopping cart via a modal interface.


๐Ÿ“Ž API Used:

Fake Store API: https://fakestoreapi.com/products


๐Ÿ“‹ Features:

  • โœ… Fetch products from the Fake Store API.

  • ๐Ÿ–ผ๏ธ Displaying product category, image, title, rating, count and price in a user-friendly layout.

  • โž• Add products to cart with a button.

  • โš ๏ธ Alert user if the item is already in the cart.

  • ๐Ÿ›๏ธ Cart item count shown in the Navbar.

  • ๐ŸชŸ Modal displays all products in the cart.

  • โŒ Remove products directly from the cart modal.

  • ๐Ÿ”„ Dynamic cart updates based on user interaction.

  • ๐Ÿ“ฑ Fully responsive design for mobile and desktop.

  • ๐Ÿงผ Clean, well-documented and maintainable code.


๐Ÿงช Key Functionalities:

  • โœ… Fetch product data on load using useEffect.

  • โœ… Add items to cart using useState and prevent duplicates.

  • โœ… Show alert message ("Item already added to the cart") for duplicates.

  • โœ… Display cart modal on click of a cart button.

  • โœ… Remove individual products from the cart.

  • โœ… Recalculate cart count dynamically.


๐Ÿ›  Tech Stack:

Technology Description
ReactJS JavaScript library for building UI
JavaScript (ES6+) Logic and state management
Tailwind CSS Utility-first CSS framework
HTML5/CSS3 Markup and styling
Fake Store API External API for product data

๐Ÿ“ฆ Component Breakdown:

  • App.jsx โ€“ Main component that manages global state (cart, products).

  • Navbar.jsx โ€“ Displays the application name and a cart button displaying the cart count.

  • ProductList.jsx โ€“ Maps and displays all products.

  • ProductItem.jsx โ€“ Displays individual product with add-to-cart functionality.

  • CartModal.jsx โ€“ Modal for displaying and removing cart items.


๐Ÿ”„ State Management & Data Flow:

  • The cart state is managed in the App.jsx using React's useState hook.

  • Product data is fetched via useEffect in the ProductList.

  • Props are passed down from App.jsx to child components (Navbar, ProductList, and CartModal).


๐Ÿ’ป Responsive Design:

  • Built with Tailwind CSS.

  • Mobile-first approach.

  • Flexbox & grid layout for adaptive design.


โœ‚๏ธ Future Improvements:

  • Add quantity management per cart item.

  • Persist cart in local storage or use Redux for larger applications.

  • Integrate user authentication and backend.


๐Ÿ™‹โ€โ™‚๏ธ Author & Contact: