useLocalStorage is useState that remembers. It works exactly like useState — const [value, setValue] = useLocalStorage(key, initial) — but the value is persisted to localStorage, so it survives a page reload, and it stays in sync across browser tabs. It's the go-to hook for a theme preference, a "dismissed" flag, or a draft the user shouldn't lose.
Implement useLocalStorage(key, initialValue). On first render, read the stored value (or fall back to initialValue). setValue updates React state and writes to localStorage, accepting a value or an updater function. Listen for the storage event so a change in another tab updates this one.
function useLocalStorage(key, initialValue) {
// returns [value, setValue] — like useState, backed by localStorage.
}
const [name, setName] = useLocalStorage('name', 'Guest');
setName('Ada'); // state updates AND localStorage['name'] = '"Ada"'
// reload the page -> name is 'Ada'
const [count, setCount] = useLocalStorage('count', 0);
setCount((c) => c + 1); // functional updater, like useState
localStorage is read once on mount, not on every render. Parse the stored JSON; fall back to initialValue if the key is absent.setValue must update state and JSON.stringify the value into localStorage.setValue(prev => next) like useState.storage event (fired in other tabs) and update state when it matches your key; clean up the listener on unmount.window is undefined) and fall back gracefully.