aboutsummaryrefslogtreecommitdiffstats
path: root/src/context/CartContext.js
blob: 0394bc06aa97af16851060ed04cc284e634774f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import React, { createContext, useState } from 'react';

export const CartContext = createContext();

export const CartProvider = ({ children }) => {
  const [cart, setCart] = useState([]);

  const addToCart = (product) => {
    setCart([...cart, product]);
  };

  const updateQuantity = (productId, quantity) => {
    setCart(cart.map(item => item.id === productId ? { ...item, quantity } : item));
  };

  return (
    <CartContext.Provider value={{ cart, addToCart, updateQuantity }}>
      {children}
    </CartContext.Provider>
  );
};