diff --git a/src/app/cartContext.tsx b/src/app/cartContext.tsx index 39b67af..d3e97df 100644 --- a/src/app/cartContext.tsx +++ b/src/app/cartContext.tsx @@ -22,17 +22,16 @@ interface CartContextType { const CartContext = createContext(undefined); export const CartProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { - const [cartItems, setCartItems] = useState([]); - - useEffect(() => { - // Load cart from localStorage on mount + const [cartItems, setCartItems] = useState(() => { + // Load cart from localStorage on initial render if (typeof window !== "undefined") { const storedCart = localStorage.getItem("shopwel_cart"); if (storedCart) { - setCartItems(JSON.parse(storedCart)); + return JSON.parse(storedCart); } } - }, []); + return []; + }); useEffect(() => { // Save cart to localStorage whenever it changes