useLocalStoragebrowser
useLocalStorage
NewSSR SAFESyncs state with localStorage across browser sessions.
$ pnpm dlx shadcn@latest add @byphongphan/use-local-storageRequires
@byphongphan in components.jsonuse-local-storage.tsx
Loading preview...
Installation
$ pnpm dlx shadcn@latest add @byphongphan/use-local-storageRequires
@byphongphan in components.jsonUsage
Props & Parameters
| Param | Type | Default | Description |
|---|---|---|---|
key | string | — | Unique key identifier in browser localStorage. |
initialValue | T | (() => T) | — | Fallback initial value if no stored value exists in localStorage. |
options.serializer | (value: T) => string | JSON.stringify | Custom serializer function for complex objects or models. |
options.deserializer | (value: string) => T | JSON.parse | Custom deserializer function. |
Returns
| Return | Type | Description |
|---|---|---|
storedValue | T | Current synchronized state value from localStorage. |
setValue | (value: T | ((prev: T) => T)) => void | Setter function supporting direct values and functional updater callbacks. |
removeValue | () => void | Removes the key from localStorage and resets state back to initial value. |