Persist state to the browser's localStorage. Syncs state across multiple tabs.
const [value, setValue] = useLocalStorage('key', 'initialValue');
Saved value: —
Try refreshing the page, the state is persisted!
import { useLocalStorage } from 'kitzo'; function ThemeToggle() { const [theme, setTheme] = useLocalStorage('theme', 'light'); return ( <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}> Toggle Theme: {theme} </button> ); }
| Parameter | Type | Description |
|---|---|---|
key | string | The localStorage key to store the value under |
initialValue | any | The initial value to use if no value exists in localStorage |
options.debounceMs | number | Delay in ms to debounce writes to localStorage (default: 0) |
options.onMount | function | Callback that runs when the initial value is read from localStorage |
Returns an array containing the current state value and a setter function, similar to useState.
storage events, syncing state across browser tabs.debounceMs option.