Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Context/ShopContext.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React, { createContext, useState } from "react";
import React, { createContext, useState, useEffect } from "react";
import all_product from "../Components/Assets/all_product";

export const ShopContext = createContext(null);

const ShopContextProvider = (props) => {
const [cartItems, setcartItems] = useState([]);
const [theme,setTheme]=useState("dark");
const [theme, setTheme] = useState(() => {
// Initialize theme from localStorage or default to "dark"
const savedTheme = localStorage.getItem('shopnex-theme');
return savedTheme || "dark";
});
const addToCart = (itemId, size, quantity) => {
const existingCartItemIndex = cartItems.findIndex(item => item.id === itemId && item.size === size);

Expand Down Expand Up @@ -42,6 +46,11 @@ const ShopContextProvider = (props) => {
return cartItems.length;
};

// useEffect hook for localStorage persistence of theme
useEffect(() => {
localStorage.setItem('shopnex-theme', theme);
}, [theme]);

const contextValue = {
all_product,
cartItems,
Expand Down