Skip to content

useSessionStorageState ​

A hook for storing state in sessionStorage. It is useful for form drafts, filters, and other temporary values that should survive refreshes but stay scoped to the current browser session.

Code demonstration ​

Basic usage ​

Store complex values ​

API ​

The usage is the same as useLocalStorageState; the storage backend is the current session's sessionStorage.

typescript
interface Options<T> {
  defaultValue?: T | (() => T)
  serializer?: (value: T) => string
  deserializer?: (value: string) => T
}

const [state, setState] = useSessionStorageState<T>(
  key: string,
  options: Options<T>
): [T?, (value?: T | ((previousState: T) => T)) => void]

Source ​

Source · Document · Demo

Released under the MIT License.