import {useState} from 'react'; export function useStoredState(storageKey: string, initialState: () => string): [string, ((newState: string) => void)] { const [state, setState] = useState(() => localStorage.getItem(storageKey) || initialState()); const setSavedState = (newState: string) => { localStorage.setItem(storageKey, newState); setState(newState); }; return [state, setSavedState]; } export function useStoredObjectState( storageKey: string, initialState: () => T ): [T, (mutator: (oldState: T) => T) => void] { const getInitialState = () => { const localStorageJson = localStorage.getItem(storageKey); if (localStorageJson !== null && localStorageJson !== '') { return JSON.parse(localStorageJson) as T; } return initialState(); }; const [state, setState] = useState(getInitialState); const setSavedState = (mut: (oldState: T) => T) => { const newState = mut(state); localStorage.setItem(storageKey, JSON.stringify(newState)); setState(newState); }; return [state, setSavedState]; }