useSessionStorageState

State Management

A React hook that manages state synchronized with sessionStorage, persisting data across page refreshes but clearing when the tab closes.

Installation

npm install @thibault.sh/hooks

API Reference

Signature

function useSessionStorageState<T>(
  key: string, 
  initialValue: T
): [T, (value: T | ((val: T) => T)) => void]

Parameters

NameTypeDescriptionDefault
key
string
The sessionStorage key to store the value under.-
initialValue
T
The default value used when no stored value exists.-

Returns

[T, (value: T | ((val: T) => T)) => void]

A tuple containing the current stored value and a setter function.

Properties:

PropertyTypeDescription
[0]
T
The current stored value (or initial value if none exists).
[1]
(value: T | ((val: T) => T)) => void
A setter function that updates both state and sessionStorage.
useSessionStorageState Hook - React Session Storage State