useSessionStorage
Persists reactive state in window.sessionStorage with automatic serialization and synchronization.
Loading preview...
The useSessionStorage hook synchronizes React state with window.sessionStorage using useSyncExternalStore, keeping all components sharing the same key in sync within the current browser tab.
Installation
Usage
Examples
Persisting Multi-Step Wizard State Across Reloads
Store wizard form data in sessionStorage so page refreshes preserve progress without leaking state to other browser tabs.
Custom Serialization Options
Pass serializer and deserializer functions when storing custom data structures in sessionStorage.
API Reference
StorageOptions
Configuration options for custom serialization and error handling.
Property
Specification
serializer?functiondefault:JSON.stringify
Custom serializer function
Full Signature
Parameters
(valueTdeserializer?functiondefault:JSON.parse
Custom deserializer function
Full Signature
Parameters
(valuestringinitializeWithValue?uniondefault:true
Read from storage immediately on initial client render
Full Signature
onError?function
Callback invoked when reading, writing, or parsing storage throws an error
Full Signature
Parameters
(errorunknown