JiyaGupta-cs / ShopNex

React Ecommerce App
https://shopnex.vercel.app
90 stars 183 forks source link

Fix: Dark mode toggles off on page refresh #499

Open maheshsemwal opened 2 weeks ago

maheshsemwal commented 2 weeks ago

Title and Issue number

Title : Dark mode gets turned off on refreshing page | Fixed Description: This PR fixes the issue where the dark mode setting gets turned off when the page is refreshed. The theme preference is now properly saved and loaded from localStorage. Issue No. : #472

Close #472

Changes Made

The changes i made to this part of the code ensure that the theme is correctly initialized from localStorage and that any changes to the theme are persisted back to localStorage. This allows the application to remember the user's theme preference even after the page is refreshed.

Original Code:

const [theme, setTheme] = useState("dark");

Updated Code:

const [theme, setTheme] = useState(() => {
    return localStorage.getItem('theme') || 'light';
});

useEffect(() => {
    localStorage.setItem('theme', theme);
}, [theme]);

Screenshots

before

https://github.com/JiyaGupta-cs/ShopNex/assets/105265591/58f2d4db-95a0-48ab-ac1a-86f6fda08f37

After

https://github.com/JiyaGupta-cs/ShopNex/assets/105265591/cf517247-e721-4241-8c53-f8e4ecc86822

Checklist

I'm contributing this under GirlScript summer of code. Please review my PR and let me know if any changes are required.

vercel[bot] commented 2 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
shop-nex ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 8, 2024 3:25pm