useLocalStorageState

State Management

A React hook that manages state synchronized with localStorage, providing automatic persistence and SSR compatibility with a useState-like interface.

Installation

npm install @thibault.sh/hooks

API Reference

Signature

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

Parameters

NameTypeDescriptionDefault
key
string
The localStorage key to use for persistence.-
initialValue
T
The default value to use if 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
Current stored value from localStorage or initial value.
[1]
(value: T | ((val: T) => T)) => void
Setter function that updates both state and localStorage.