useLocalStorage
SSR-safe state, persisted and synced across tabs.
useLocalStorage is a useState-like hook whose value persists to
localStorage. It's SSR-safe, syncs across browser tabs via the storage event,
and syncs other components in the same tab. A useSessionStorage variant backs
onto sessionStorage.
Usage
It returns a [value, setValue, remove] tuple. setValue accepts a value or an
updater function, just like useState:
Signature
Values are JSON-serialized by default. Pass a serializer/deserializer to store
a raw string instead — for example a value an inline script must read without
parsing JSON:
SSR-safe by design: the server and first client render return defaultValue, then the stored value resolves on
hydration. Writes are wrapped in try/catch, so private-mode or quota failures won't crash your app. Need to pick the
backend at runtime? Use the lower-level useStorage('local' | 'session', …).