Skip to main content

useLocalStorage

A useState-like hook that persists state to localStorage. The value is automatically serialized/deserialized as JSON. Falls back to the initial value if the storage key is missing or parsing fails.

Import

import { useLocalStorage } from '@andrejground/lab';

Usage

import { useLocalStorage } from '@andrejground/lab';

export default function App() {
const [theme, setTheme] = useLocalStorage('app-theme', 'light');

return (
<div>
<p>Current theme: {theme}</p>
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Toggle theme
</button>
</div>
);
}

Functional updates

The setter supports functional updates just like useState:

const [count, setCount] = useLocalStorage('counter', 0);

setCount((prev) => prev + 1);

Reading the latest value

The third returned value is a getValue getter that reads directly from localStorage, useful when you need the freshest value outside of React's render cycle:

const [value, setValue, getValue] = useLocalStorage('key', 'default');

// getValue() reads directly from localStorage
console.log(getValue());

API

const [value, setValue, getValue] = useLocalStorage(key, initialValue);
ParameterTypeDescription
keystringThe localStorage key
initialValueTFallback value if the key does not exist

Returns

IndexTypeDescription
[0]TThe current stored value
[1](value: T | (prev: T) => T) => voidSetter (supports functional updates)
[2]() => TGetter that reads directly from localStorage