useLocalStorage
A custom React hook that persists state in localStorage
and syncs across browser tabs.
- Allows data persistence between page refreshes and browser sessions
- Automatically syncs state across browser tabs with the same site open
- Uses the same API as React's useState hook for a familiar developer experience
Installation
The useLocalStorage hook is part of our UI library. You can import it directly from the hooks directory.
Usage
Here is how to use the useLocalStorage hook in your project:
React TSX
React TSX
API Reference
React TSX
Inputs
key
: The key to store the value under in localStorageinitialValue
: The initial value to use if no value is found in localStorage
Output
value
: The current state valuesetValue
: Function to update the state (works like useState setter)
Behavior
- First render retrieves the value from localStorage (or uses initialValue if not found)
- Any updates through the setter will persist to localStorage
- Changes to localStorage from other tabs or windows will update the state automatically
- The hook handles serialization to JSON for storage and parsing on retrieval
Implementation
React TSX