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