From 57644764a2e90c0a0bc5b7d911204e3175325633 Mon Sep 17 00:00:00 2001 From: "gitauto-for-dev[bot]" <160085510+gitauto-for-dev[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 00:29:08 +0000 Subject: [PATCH] Update src/context/ThemeContext.js. --- src/context/ThemeContext.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/context/ThemeContext.js diff --git a/src/context/ThemeContext.js b/src/context/ThemeContext.js new file mode 100644 index 0000000..478ee90 --- /dev/null +++ b/src/context/ThemeContext.js @@ -0,0 +1,26 @@ +import React, { createContext, useState, useEffect } from 'react'; + +export const ThemeContext = createContext(); + +export const ThemeProvider = ({ children }) => { + const [theme, setTheme] = useState('light'); + + useEffect(() => { + const savedTheme = localStorage.getItem('theme'); + if (savedTheme) { + setTheme(savedTheme); + } + }, []); + + const toggleTheme = () => { + const newTheme = theme === 'light' ? 'dark' : 'light'; + setTheme(newTheme); + localStorage.setItem('theme', newTheme); + }; + + return ( + + {children} + + ); +};