Storage

You can restore the value of a named store to browser storage.


storage options

If you omit the storage option, it will reuse the existing storage alias for that name, and if there is no alias, in-memoryis the default. Specify storage when persistence is needed.

ts
type RxStateStorage =
  | "in-memory"
  | "auto"
  | "indexeddb"
  | "IndexedDB"
  | "websql"
  | "WebSQL"
  | "localstorage"
  | "localStorage"
  | "sessionstorage"
  | "sessionStorage";

Lowercase storage names can be used in their basic form, and aliases for IndexedDB, WebSQL, localStorage, and sessionStorage are normalized to the same backend.

ts
createRxState(0, "count");
// alias가 없으므로 in-memory

createRxState(0, "count", { storage: "auto" });
// 같은 name이어도 storage가 다르면 별도 store

createRxState(0, "theme", { storage: "localstorage" });
createRxState("", "draft", { storage: "sessionstorage" });
createRxState("", "draftAlias", { storage: "sessionStorage" });

sessionstorage

sessionstorage and sessionStorage uses the browser's sessionStorage. Values are maintained within the current tab/session scope and will hydrate if recreated with the same name and storage option.

tsx
const { setDraft, useDraft } = createRxState(
  "",
  "draft",
  { storage: "sessionStorage" },
);

function DraftInput() {
  const draft = useDraft();

  return (
    <input
      value={draft}
      onChange={(event) => setDraft(event.target.value)}
    />
  );
}

Example of saving user settings

The example below saves UI preferences to localStorage. If recreated with the same name and storage option after a refresh, it hydrates the saved value.

tsx
const { setPrefs, usePrefs, prefsReady } = createRxState(
  { compact: false, showHints: true },
  "prefs",
  { storage: "localstorage" },
);

function PreferencesPanel() {
  const prefs = usePrefs();

  return (
    <button onClick={() => setPrefs((prev) => ({ ...prev, compact: !prev.compact }))}>
      compact: {String(prefs.compact)}
    </button>
  );
}
© 2026 Byeolnaerim. All rights reserved.IntroductionPrivacy Policy