Update src/app/cartContext.tsx
This commit is contained in:
@@ -22,17 +22,16 @@ interface CartContextType {
|
||||
const CartContext = createContext<CartContextType | undefined>(undefined);
|
||||
|
||||
export const CartProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [cartItems, setCartItems] = useState<CartItem[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
// Load cart from localStorage on mount
|
||||
const [cartItems, setCartItems] = useState<CartItem[]>(() => {
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user