Local Storage¶
Introduction¶
@parity/product-sdk-local-storage is an async key-value store backed by the Host container's storage. It gives your Product a small, per-device place to keep state — settings, drafts, cached identifiers — with optional key namespacing and typed JSON helpers, without touching raw browser localStorage.
The store is scoped per Product, so keys never collide with other Products, and reads are error-tolerant: a missing or failed read resolves to null rather than throwing.
When to Use It¶
- To persist small app state inside a Host: preferences, drafts, cached values, or a session identifier.
- To namespace keys per Product with a
prefix, sothemebecomesmy-product:themeand stays isolated. - To hand a store to higher-level SDK pieces; for example, the session-key manager in Keys takes a store to persist its mnemonic.
- Not a general-purpose browser shim: the store requires a Host and has no standalone browser fallback. For raw Host storage without the key-value convenience layer, use the Host package directly.
Core Concepts¶
createLocalKvStore(options): The single factory. It is async because it detects the Host storage backend, and it returns aLocalKvStore.LocalKvStore: The returned store. It exposesget,set, andremovefor strings, plusgetJSONandsetJSONfor typed JSON values.- Namespacing: Pass a
prefixto isolate this Product's keys from everything else in the Host's storage. - Error-tolerant reads:
getandgetJSONreturnnullfor a missing key or a failed read; writes and removes log failures rather than throwing. Only the factory throws, when no Host is present.
Persist and Read App State¶
Create a namespaced store, then read and write both strings and JSON:
import { createLocalKvStore } from '@parity/product-sdk-local-storage';
const store = await createLocalKvStore({ prefix: 'my-product' });
await store.set('theme', 'dark');
const theme = await store.get('theme'); // string | null
await store.setJSON('draft', { title: 'Untitled', body: '' });
const draft = await store.getJSON<{ title: string; body: string }>('draft');
await store.remove('draft');
Limitations¶
createLocalKvStorethrows if no Host storage is detected; the store is Host-only.- Write and remove failures are logged, not thrown, so a failed
setlooks like success to the caller. - Reads never throw: both errors and missing keys surface as
null.
Where to Go Next¶
-
Guide Persist Data Locally
The task-focused recipe: JSON helpers, prefixes, and React usage, step by step.
-
Learn Keys
A common consumer of this store: the session-key manager persists its mnemonic here.
-
External API Reference
The complete
local-storagesurface:createLocalKvStoreandLocalKvStore.
| Created: September 2, 2026