Documentation Index
Fetch the complete documentation index at: https://mintlify.com/rijvi-mahmud/shaddy/llms.txt
Use this file to discover all available pages before exploring further.
useSessionStorage is a React hook that provides a useState-style interface for persisting values in the browser’s sessionStorage. It has an identical API to useLocalStorage — JSON serialization, a setter that accepts values or updater functions, and a remover — with the key difference that session storage is scoped to the current browser tab and is automatically cleared when the tab is closed. It is ideal for temporary UI state that should survive page refreshes within a session but must not bleed into other tabs or persist permanently.
Installation
Signature
Parameters
The sessionStorage key to store the value under. Each unique key is an independent storage slot within the current tab’s session.
The value (or a factory function returning the value) to use when no entry exists in sessionStorage for the given
key. Defaults to undefined.Optional configuration object.
Return Value
The current value from sessionStorage, or
initialValue when no entry exists for the key. Always typed as T.A setter function with the same API as React’s
useState setter. Accepts a new value directly or an updater function (prev: T) => T. Writes to sessionStorage and updates React state atomically.Removes the item from sessionStorage and resets the React state to
initialValue.Usage
Persisting a Multi-Step Form
Remembering a Tab Selection Within a Session
Notes
sessionStorageis per-tab and per-origin. Data written in one tab is not accessible in another tab, and it is cleared automatically when the tab is closed.- The hook attaches a
storageevent listener (viauseEventListener) to keep the in-memory state consistent if the stored value is modified externally. Note that browsers do not firestorageevents for sessionStorage across different tabs, so no cross-tab sync occurs in practice. - The hook is safe to use in SSR environments — when
window.sessionStorageis unavailable it falls back toinitialValueand callsonErrorrather than throwing. - If you need data to persist beyond a single session (across tab closes and browser restarts), use
useLocalStorageinstead.