State Management

Updated 8 July 2026

The project uses React Context API for global state management. This provides a simple, effective way to manage application state without external libraries.

Why React Context?

Simple & Lightweight

No external dependencies, built into React

Perfect for E-commerce

Ideal for cart and wishlist state that needs to be shared across components

Server-Side Compatible

Works seamlessly with Next.js Server Components

Cart Context

Filesrc/contexts/CartContext.jsx

The Cart Context manages all shopping cart state and operations:

Using Cart Context

import { useCart } from "@/contexts/CartContext"; // Import Cart context

function MyComponent() { // My component to use cart context
  const { addToCart, updateQuantity, removeItem, cartData } = useCart(); // Get add to cart, update quantity, remove item and cart data from cart context
  
  await addToCart(productId, quantity); // Add product to cart with the given product id and quantity
  await updateQuantity(itemKey, newQuantity); // Update quantity of the item with the given key to the new quantity
  await removeItem(itemKey); // Remove item with the given key from the cart
}

Cart Context Properties

Add to Cart Example

"use client";

import { useCart } from "@/contexts/CartContext"; // Import Cart context

export default function AddToCartButton({ productId }) { // Client component to add to cart
  const { addToCart, isUpdating } = useCart();
  
  return (
     addToCart(productId, 1)} disabled={isUpdating}>
      Add to Cart
    
  );
}

Update Quantity Example

const { updateQuantity } = useCart(); // Get update quantity function from cart context

 updateQuantity(itemKey, quantity + 1)}>+
 updateQuantity(itemKey, quantity - 1)}>-

Wishlist Context

Filesrc/contexts/WishlistContext.jsx

The Wishlist Context manages wishlist items stored in browser cookies:

Using Wishlist Context

import { useWishlist } from "@/contexts/WishlistContext";

const { toggle, isInWishlist } = useWishlist();

toggle(productId);
if (isInWishlist(productId)) { /* in wishlist */ }

Wishlist Context Properties

Wishlist Button Example

const { toggle, isInWishlist } = useWishlist(); // Get toggle and isInWishlist functions from wishlist context

 toggle(productId)}> // Toggle product in/out of wishlist with the given product id

Provider Setup

// app/layout.js
import { CartProvider } from "@/contexts/CartContext"; // Import Cart provider
import { WishlistProvider } from "@/contexts/WishlistContext"; // Import Wishlist provider

export default function RootLayout({ children }) { // Root layout to wrap pages and persist across navigation
  return (
     // Wrap pages with Cart provider
       // Wrap pages with Wishlist provider
        {children}
       // Wrap pages with Wishlist provider
    
  ); // Return wrapped pages
}

Optimistic Updates

The Cart Context uses optimistic updates for better UX:

Benefits: Optimistic updates make the app feel faster and more responsive, even with network latency.

State Persistence

Cart State

Wishlist State

Best Practices