persist-pinia
Persist a Pinia store instance with @stainless-code/persist (persistStore). Use when wiring Pinia option/setup stores to storage; hydrate applies via $state = (not $patch), with default shallow merge.
- License 许可
- MIT
- 最近更新
- 2026-11-01
- Stars
- ★ 5
- Forks
- 0
01 前言 Front Matter
- name
- persist-pinia
- license
- MIT
- sources
- [ "stainless-code/persist:src/adapters/sources/pinia.ts", "stainless-code/persist:docs/architecture.md" ]
- metadata
- { "type": "composition", "library": "@stainless-code/persist", "framework": "pinia", "library_version": "0.4.1" }
- description
- Persist a Pinia store instance with @stainless-code/persist (persistStore). Use when wiring Pinia option/setup stores to storage; hydrate applies via $state = (not $patch), with default shallow merge.
03 SKILL.md 正文
Persisting Pinia
@stainless-code/persist/sources/pinia maps a store instance onto persistSource. Hydrate applies via $state = (not $patch); default merge still shallow-spreads, so keys absent from the payload can remain. Subscribe uses $subscribe(…, { detached: true }) so the listener survives Vue effectScope stop.
When to use this skill
- You have
const store = useX()and want reload survival for option or setup stores. - You need Persist's gate / codecs / cross-tab instead of (or beside) pinia-plugin-persistedstate.
Install
bun add @stainless-code/persist pinia
pinia is an optional peer of /sources/pinia (Vue app + createPinia() still required by Pinia).
Minimal wiring
import { defineStore } from "pinia";
import { createJSONStorage } from "@stainless-code/persist";
import { persistStore } from "@stainless-code/persist/sources/pinia";
const usePrefs = defineStore("prefs", {
state: () => ({ theme: "light" as const }),
});
const store = usePrefs(); // instance, after pinia is active
const persist = persistStore(store, {
name: "app:prefs:v1",
storage: createJSONStorage(() => localStorage),
});
Common mistakes
- Passing
defineStore(...)/ the factory instead ofusePrefs()instance. - Expecting
$patchsemantics on hydrate. Persist assigns$stateaftermerge. - Name clash with Redux
persistStore. Import from/sources/pinia(or alias). - Call before
setActivePinia/ app.use(pinia). Same as any Pinia store use.
API surface
persistStore(store, options) → PersistApi—storeis a PiniaStore- Options /
PersistApi: same aspersistSource
See also: react-persist / Vue hydration adapters for UI gating on async backends.
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.