diff --git a/src/hooks/useLocalStorage.ts b/src/hooks/useLocalStorage.ts new file mode 100644 index 0000000..7ab69f2 --- /dev/null +++ b/src/hooks/useLocalStorage.ts @@ -0,0 +1,18 @@ +import { useEffect, useState } from "react" + +export function useLocalStorage(key: string, initialValue: T) { + const [value, setValue] = useState(() => { + try { + const stored = localStorage.getItem(key) + return stored ? JSON.parse(stored) : initialValue + } catch { + return initialValue + } + }) + + useEffect(() => { + localStorage.setItem(key, JSON.stringify(value)) + }, [key, value]) + + return [value, setValue] as const +} \ No newline at end of file