diff options
author | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-10-16 16:18:13 +0530 |
---|---|---|
committer | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-10-16 16:18:13 +0530 |
commit | 9b82cab815d31fb57d5a337debb59e3458af1f0b (patch) | |
tree | 77d6a341a1945aa87375fc466bab2bdb99a5030c /src | |
parent | cb65e181d120d2d9ff0672e12fc2db3b16d85d1c (diff) | |
download | mall-app-9b82cab815d31fb57d5a337debb59e3458af1f0b.tar.gz mall-app-9b82cab815d31fb57d5a337debb59e3458af1f0b.tar.bz2 mall-app-9b82cab815d31fb57d5a337debb59e3458af1f0b.zip |
Add CartContext for cart management and state
Diffstat (limited to 'src')
-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> + ); +}; |