diff options
Diffstat (limited to 'src/context')
-rw-r--r-- | src/context/CartContext.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/context/CartContext.js b/src/context/CartContext.js new file mode 100644 index 0000000..0394bc0 --- /dev/null +++ b/src/context/CartContext.js @@ -0,0 +1,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> + ); +}; |