import { useCallback, useState, useEffect } from "react"; export function useLocalStorage(key: string, defaultValue: T | (() => T)) { return useStorage(key, defaultValue, window.localStorage); } export function useSessionStorage(key: string, defaultValue: T | (() => T)) { return useStorage(key, defaultValue, window.sessionStorage); } function useStorage( key: string, defaultValue: T | (() => T), storageObject: Storage ): [T, React.Dispatch>, () => void] { const [value, setValue] = useState(() => { const value = storageObject.getItem(key); if (value != null) return JSON.parse(value); if (typeof defaultValue === "function") { const value = (defaultValue as () => T)(); return value; } else { return JSON.parse(JSON.stringify(defaultValue)); } }); useEffect(() => { if (value === undefined) return storageObject.removeItem(key); storageObject.setItem(key, JSON.stringify(value)); }, [key, value, storageObject]); const remove = useCallback(() => { setValue(undefined as unknown as T); }, []); return [value, setValue, remove]; }