Skip to main content

Overview

The CartContext provides centralized state management for the shopping cart, product catalog, and search functionality. It handles cart operations like adding, removing, and updating products, as well as fetching products from the API. Source: ~/workspace/source/src/context/CartContext.jsx

Setup

Wrap your application with the CartProvider to make the context available to all components:

Usage

Access the cart context in any component:

State Properties

cart

array
Array of product objects currently in the shopping cart. Each item includes the product data plus a cantidad (quantity) property.

productos

array
Complete product catalog fetched from the API. Updated on component mount.

productosFiltrados

array
Filtered product list based on the current search query (busqueda). Automatically filters by product name (case-insensitive).

cartCount

number
Number of unique items in the cart (not total quantity).

isCartOpen

boolean
Controls the visibility of the cart drawer/modal.

setCartOpen

function
Function to toggle cart drawer visibility.

busqueda

string
Current search query string.

setBusqueda

function
Update the search query. Automatically filters productosFiltrados.

carga

boolean
Loading state while fetching products from the API. Initially true, becomes false after 2-second delay.

error

boolean
Error state if product fetching fails.

Methods

handleAddToCart

Adds a product to the cart or increments quantity if already present. Displays toast notifications.
object
required
Product object to add. Must include cantidad property set to 1 for new additions.
Behavior:
  • If product exists in cart: increments cantidad by 1
  • If product is new: adds to cart with specified cantidad
  • Shows success toast for new items
  • Shows info toast for quantity increases
Example:
src/components/Product.jsx

eliminarProducto

Completely removes a product from the cart regardless of quantity.
object
required
Product object with id property to identify which item to remove.
Example:
src/components/Cart.jsx

borrarProducto

Decrements the quantity of a product by 1. Removes the product entirely if quantity reaches 0.
object
required
Product object with id and cantidad properties.
Behavior:
  • If cantidad > 1: decrements by 1
  • If cantidad === 1: removes product from cart
Example:
src/components/Cart.jsx

vaciarCarrito

Clears all items from the cart. No parameters Example:
src/components/Cart.jsx

Complete Usage Example

Here’s a full example from the Cart component showing all cart operations:
src/components/Cart.jsx

API Endpoint

The context fetches products from:
Fetch occurs on mount with a 2-second simulated delay for loading state demonstration.

Implementation Details

  • Toast Notifications: Uses react-toastify for add-to-cart feedback
  • Local State: Cart state is stored in component state (not persisted)
  • Auto-filtering: Search filter updates automatically when busqueda changes
  • Error Handling: Displays error message if API fetch fails