useSuspenseSessionStorageState
About
⚠️ Experimental Hook: This hook may be removed or significantly changed in any release without notice.
A Suspense-enabled hook for sessionStorage state management with proper serialization. This hook suspends during initialization and must be wrapped in a Suspense boundary. It provides full TypeScript generic support, proper JSON serialization/deserialization, error handling, and automatic state management for session-scoped data.
Unlike localStorage, sessionStorage data is scoped to the current browser tab/session and doesn't persist across browser restarts.
Examples
Basic Usage with Numbers
Working with Objects and TypeScript
Session-Scoped Shopping Cart
Multi-Step Form with Session Persistence
With Error Boundary for Production Use
SessionStorage vs LocalStorage
Key differences between useSuspenseSessionStorageState
and useSuspenseLocalStorageState
:
Feature | SessionStorage | LocalStorage |
---|---|---|
Persistence | Tab/session only | Survives browser restarts |
Scope | Single tab | All tabs of same origin |
Cross-tab Sync | No (tab-specific) | Yes |
Use Cases | Form drafts, tab settings, temporary data | User preferences, persistent app state |
Data Lifetime | Until tab is closed | Until explicitly deleted |
Arguments
Parameter | Type | Description |
---|---|---|
key | string | The sessionStorage key to use for storing the data |
initializer | (currentValue: string | null) => T | Function that transforms the raw sessionStorage value (string or null) into type T |
Initializer Function
The initializer function receives the raw string value from sessionStorage (or null
if not present) and should return the properly typed initial state. This allows for:
- Type Conversion: Convert strings to numbers, parse JSON objects, etc.
- Default Values: Provide fallbacks when sessionStorage is empty
- Data Validation: Validate and sanitize stored data
- Migration: Handle changes in data structure over time
Return Value
Returns a tuple [value, controls]
where:
Property | Type | Description |
---|---|---|
value | T | The current state value |
controls | object | Object containing control methods |
Control Methods
The controls
object provides the following methods:
Method | Type | Description |
---|---|---|
getItem | () => T | Returns the current state value |
setItem | (value: T) => void | Updates the state and sessionStorage with the new value |
deleteItem | () => void | Removes the item from sessionStorage and resets to initial value |
Type Safety
The hook provides full TypeScript generic support:
Error Handling
The hook handles various error scenarios gracefully:
- sessionStorage Unavailable: Works in SSR environments or when sessionStorage is disabled
- Storage Quota Exceeded: Continues to work even if sessionStorage writes fail
- JSON Parse Errors: Lets the initializer handle malformed data
- Initializer Errors: Throws errors to be caught by error boundaries
Always wrap in error boundaries for production use.
Common Use Cases
Form Draft Auto-Save
Temporary Shopping Cart
Tab-Specific UI State
Browser Support
- Modern Browsers: ✅ Full support
- Server-Side Rendering: ✅ Works (uses initializer default when sessionStorage unavailable)
- Storage Disabled: ✅ Graceful fallback (state works, but won't persist)