VisorVisor
Hooks

useLocalStorage

Persists state to localStorage with SSR safety and cross-tab synchronization.

Usage

const [theme, setTheme, removeTheme] = useLocalStorage("theme", "light")

Installation

npx visor add use-local-storage

Parameters

NameTypeRequiredDefaultDescription
keystringYes--The localStorage key to read from and write to.
initialValueTYes--Default value used when no stored value exists or localStorage is unavailable.

Returns

NameTypeDescription
storedValueTCurrent value from localStorage (or the initial value as fallback).
setValue(value: T | ((prev: T) => T)) => voidUpdates the stored value. Accepts a direct value or an updater function.
removeValue() => voidRemoves the key from localStorage and resets to the initial value.