Swirski UI

Hook

useLocalStorage

Persists React state in localStorage with serializer hooks.

Import

import { useLocalStorage } from "@swirski/ui";

Preview

Live render

useLocalStorage

Preview

Persistent settings

Good for cursor choice, theme choice and docs preferences.

Usage

const [cursor, setCursor, clearCursor] = useLocalStorage(
  "swirski-cursor",
  "bolt",
);

Props

4 documented
PropTypeDefaultNotes
keyRequiredstring-localStorage key.
defaultValueRequiredT-Fallback value.
options.serialize(value: T) => string-Custom serializer.
options.deserialize(value: string) => T-Custom parser.

Returns

3 documented
ValueTypeNotes
valueTCurrent state, initialized from defaultValue and then hydrated from localStorage.
setValueDispatch<SetStateAction<T>>Updates React state and persists the serialized value.
removeValue() => voidRemoves the stored item and resets state to defaultValue.
Previous: useIsPathnameActiveNext: useMediaQuery