The localStorage key to persist under
The initial value when no stored value exists
A tuple of [value, setValue] identical to useState
Works like useState but automatically reads from and writes to localStorage.
Supports SSR environments by guarding against window access. Corrupted
localStorage values fall back to the provided default. The storage event
listener keeps multiple tabs in sync.
import { usePersistedState } from '@hiyve/utilities';
function JoinForm() {
const [userName, setUserName] = usePersistedState('hiyve-userName', '');
const [roomName, setRoomName] = usePersistedState('hiyve-roomName', '');
return (
<>
<input value={userName} onChange={(e) => setUserName(e.target.value)} />
<input value={roomName} onChange={(e) => setRoomName(e.target.value)} />
</>
);
}
React hook that persists state to localStorage with cross-tab synchronization.