{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "use-local-storage", "title": "useLocalStorage", "description": "React hook that syncs state with localStorage, with automatic JSON serialization.", "dependencies": [ "react" ], "files": [ { "path": "registry/better-resizable-panes/use-local-storage.ts", "content": "import { useState, useCallback } from \"react\";\n\n/**\n * React hook that syncs state with localStorage.\n * Reads the initial value from localStorage (falling back to the provided default),\n * and writes back on every update.\n */\nexport function useLocalStorage(\n key: string,\n initialValue: T,\n): [T, (value: T | ((prev: T) => T)) => void] {\n const [stored, setStored] = useState(() => {\n try {\n const raw = localStorage.getItem(key);\n return raw != null ? JSON.parse(raw) : initialValue;\n } catch {\n return initialValue;\n }\n });\n\n const setValue = useCallback(\n (value: T | ((prev: T) => T)) => {\n setStored((prev) => {\n const next = value instanceof Function ? value(prev) : value;\n try {\n localStorage.setItem(key, JSON.stringify(next));\n } catch {\n /* quota exceeded - silently ignore */\n }\n return next;\n });\n },\n [key],\n );\n\n return [stored, setValue];\n}\n", "type": "registry:hook", "target": "hooks/use-local-storage.ts" } ], "type": "registry:hook" }