React hook for persistent state in localStorage
Hookshooklocal-storagestate-persistencereacttypescript
A custom React hook that synchronizes component state with browser localStorage, supporting functional updates and error resilience.
This hook lets you persist React component state in localStorage with the same ergonomic API as useState. It safely parses stored JSON on init and handles errors during read/write. To use it, call const [value, setValue] = useLocalStorage('my-key', defaultValue) — then use value and setValue just like useState. Note: values must be JSON-serializable; non-serializable types (e.g., functions, Date objects without string conversion) will cause silent failures or unexpected behavior. The hook does not auto-sync across tabs — for that, listen to storage events separately.