useSuspenseLocalStorageState
About
⚠️ Experimental Hook: This hook may be removed or significantly changed in any release without notice.
A Suspense-enabled hook for localStorage state management with cross-tab synchronization. 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 synchronization across browser tabs.
Examples
Basic Usage with Numbers
Working with Objects and TypeScript
Cross-tab Synchronization Demo
Complex State with Arrays
With Error Boundary for Production Use
Cross-tab Synchronization
The hook automatically synchronizes state across browser tabs and windows through:
- Storage Events: Listens for
storage
events fired when localStorage is modified in other tabs - Custom Events: Uses custom document events to synchronize state changes within the same tab across multiple hook instances
- Real-time Updates: Components automatically re-render when localStorage changes occur from any source
This means if you have the same hook with the same key in multiple tabs or multiple components, they will all stay in sync automatically.
Arguments
Parameter | Type | Description |
---|---|---|
key | string | The localStorage key to use for storing the data |
initializer | (currentValue: string | null) => T | Function that transforms the raw localStorage value (string or null) into type T |
Initializer Function
The initializer function receives the raw string value from localStorage (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 localStorage 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 localStorage with the new value |
deleteItem | () => void | Removes the item from localStorage and resets to initial value |
Type Safety
The hook provides full TypeScript generic support:
Error Handling
The hook handles various error scenarios gracefully:
- localStorage Unavailable: Works in SSR environments or when localStorage is disabled
- Storage Quota Exceeded: Continues to work even if localStorage 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.
Browser Support
- Modern Browsers: ✅ Full support
- Server-Side Rendering: ✅ Works (uses initializer default when localStorage unavailable)
- Storage Disabled: ✅ Graceful fallback (state works, but won't persist)