Skip to Content
ohooks

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

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useSessionStorage.json

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
deserializer?
functiondefault:JSON.parse
Custom deserializer function
initializeWithValue?
uniondefault:true
Read from storage immediately on initial client render
onError?
function
Callback invoked when reading, writing, or parsing storage throws an error