persist-tanstack-store
Persist a @tanstack/store Store or writable Atom with @stainless-code/persist (persistStore/persistAtom). Use when wiring TanStack Store to storage, or choosing persistAtom replace-merge vs persistStore.
- License 许可
- MIT
- 最近更新
- 2026-11-01
- Stars
- ★ 5
- Forks
- 0
01 前言 Front Matter
- name
- persist-tanstack-store
- license
- MIT
- sources
- [ "stainless-code/persist:src/adapters/sources/tanstack-store.ts" ]
- metadata
- { "type": "composition", "library": "@stainless-code/persist", "framework": "@tanstack/store", "library_version": "0.4.1" }
- requires
- [ "persist" ]
- description
- Persist a @tanstack/store Store or writable Atom with @stainless-code/persist (persistStore/persistAtom). Use when wiring TanStack Store to storage, or choosing persistAtom replace-merge vs persistStore.
03 SKILL.md 正文
Persisting TanStack Store
Thin adapters over persistSource — shared Options / gate / throttle / cross-tab live in skill persist.
persistStore(store, options) for Store (action-bearing included). persistAtom(atom, options) for a writable Atom.
Install
bun add @stainless-code/persist @tanstack/store
# Set/Map/Date on string-wire backends:
bun add seroval
@tanstack/store is an optional peer of /sources/tanstack-store.
Minimal wiring
import { Store } from "@tanstack/store";
import { createJSONStorage } from "@stainless-code/persist";
import { persistStore } from "@stainless-code/persist/sources/tanstack-store";
const store = new Store({ theme: "light" });
const persist = persistStore(store, {
name: "app:prefs:v1",
storage: createJSONStorage(() => localStorage),
});
Set/Map/Date → createSerovalStorage from /codecs/seroval (see persist-seroval).
Keep the PersistApi for rehydrate() / destroy() / onHydrate / clearStorage(). Non-singletons must destroy() on teardown.
persistAtom vs persistStore
persistAtom only:
- Default
mergeREPLACES ((persisted) => persisted) — shallow-spreading a primitive corrupts it. Override uses??, somerge: undefinedstill replace-merges. - Throws on readonly atoms —
"[persistAtom] Cannot persist a readonly atom."
import { createAtom } from "@tanstack/store";
import { persistAtom } from "@stainless-code/persist/sources/tanstack-store";
const theme = createAtom<"light" | "dark">("light");
persistAtom(theme, { name: "app:theme:v1" });
When to drop to persistSource
- No first-party adapter for the store shape.
- You need subscription timing without the adapter's opinions.
Zustand → persist-zustand. UI gate → react-persist (or other *-persist).
API surface
persistStore(store, options) → PersistApipersistAtom(atom, options) → PersistApi(writable only; replace-merge default)
04 同仓库更多 Skills
vue-persist
Gate Vue 3 UI on Persist hydration with useHydrated (shallowRef). Use in setup/effectScope when avoiding flash of default state on async backends.
svelte-store-persist
Gate Svelte store-based UI on Persist hydration with hydratedStore (Readable). Use for Svelte 3–4 or Svelte 5 apps that still use stores; runes UI → svelte-persist.
svelte-persist
Gate Svelte 5 runes UI on Persist hydration with hydratedRune (.current). Use for Svelte 5; for stores / Svelte 3–4 use svelte-store-persist.
solid-persist
Gate Solid UI on Persist hydration with useHydrated (Accessor). Use when avoiding flash of default state on async backends; read hydrated() in reactive scopes.
react-persist
Gate React UI on Persist hydration with useHydrated + toHydrationSignal. Use when avoiding flash of default state on async backends (IndexedDB), SSR snapshot true, or wiring HydrationSignal into React 18/19.
preact-persist
Gate Preact UI on Persist hydration with useHydrated (preact/compat useSyncExternalStore). Use when avoiding flash of default state on async backends or wiring HydrationSignal into Preact.