useLocalStorage
import { useState, useEffect } from "react";
export function useLocalStorage<T>(key: string, initialValue: T) { const [storedValue, setStoredValue] = useState<T>(() => { try { const item = window.localStorage.getItem(key); return item ? (JSON.parse(item) as T) : initialValue; } catch (error) { return initialValue; } });
useEffect(() => { try { window.localStorage.setItem(key, JSON.stringify(storedValue)); } catch (error) { console.error("Error saving to localStorage:", error); } }, [key, storedValue]);
return [storedValue, setStoredValue] as const;}