From 9b82cab815d31fb57d5a337debb59e3458af1f0b Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Wed, 16 Oct 2024 16:18:13 +0530 Subject: Add CartContext for cart management and state --- src/context/CartContext.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/context/CartContext.js 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 ( + + {children} + + ); +}; -- cgit v1.2.3-59-g8ed1b