import { useState, useEffect } from 'react'; function useLocalStorage(key, initialValue) { const [storedValue, setStoredValue] = useState(() => { try { const item = window.localStorage.getItem(key); return item ? JSON.parse(item) : initialValue; } catch (error) { console.error(error); return initialValue; } }); useEffect(() => { try { window.localStorage.setItem(key, JSON.stringify(storedValue)); } catch (error) { console.error(error); } }, [key, storedValue]); return [storedValue, setStoredValue]; } // Example Usage: // function ThemeToggle() { // const [theme, setTheme] = useLocalStorage('app-theme', 'light'); // const toggleTheme = () => { // setTheme(theme === 'light' ? 'dark' : 'light'); // }; // return ( // // ); // }