useLocalStorage
useState persisted to localStorage, synced across components and tabs.
Stores a JSON value under a key. The server render and hydration use the initial value, and the stored value is read right after. Every caller with the same key stays in sync in this tab and in other tabs; blocked storage falls back to memory.
Usage
Examples
Reading preference
Callers stay in sync
Preferences follow the active key
API
5 options
3 return values
Accessibility
Persisted preferences (text size, reduced motion, dismissed notices) must still have a visible control to change them back.
Notes
- Never store secrets, tokens or personal data in localStorage.
Related
useMapMap state with immutable set, replace, remove, clear and reset actions.useReadLocalStorageRead-only localStorage subscriptions shared with useLocalStorage.useSelectionMulti-select state in a Set, with select-all and indeterminate support.useSessionStorageuseState persisted to sessionStorage for one tab.