blob: 0394bc06aa97af16851060ed04cc284e634774f2 (
plain) (
tree)
|
|
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>
);
};
|