Hiyve Components - v1.0.0
    Preparing search index...
    • React hook that persists state to localStorage with cross-tab synchronization.

      Type Parameters

      • T

      Parameters

      • key: string

        The localStorage key to persist under

      • defaultValue: T

        The initial value when no stored value exists

      Returns [T, (value: T | ((prev: T) => T)) => void]

      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)} />
      </>
      );
      }