useSessionStorage

Custom hook that uses the [`sessionStorage API`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) to persist state across page reloads.

Custom hook that uses the sessionStorage API to persist state across page reloads.

Usage

import { useSessionStorage } from "@tilt-legal/cubitt-components/utilities/hooks";

const [count, setCount, removeCount] = useSessionStorage('count', 0);
// Access the `count` value, the `setCount` function to update it and `removeCount` function to remove the key from storage.

API

Signature

declare function useSessionStorage<T>(key: string, initialValue: T | (() => T), options?: UseSessionStorageOptions<T>): [T, Dispatch<SetStateAction<T>>, () => void];

Parameters

ParameterTypeDescriptionDefault
keystringThe key under which the value will be stored in session storage.-
initialValue`T(() => T)`The initial value of the state or a function that returns the initial value.
[options]?UseSessionStorageOptions&lt;T&gt;Options for customizing the behavior of serialization and deserialization (optional).-

Return Value

ValueTypeDescription
return[T, Dispatch&lt;SetStateAction&lt;T&gt;&gt;, () =&gt; void]A tuple containing the stored value, a function to set the value and a function to remove the key from storage.

On this page