Zustand

作者 vercel-labsfc2a696a50a3無授權條款18K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫昨天更新

Zustand adapter for json-render's StateStore interface. Use when integrating json-render with Zustand for state management via @json-render/zustand.

精選僅含說明Software Development
AI 產生的概覽

將 Zustand vanilla store 接入 json-render 的 StateStore 介面的轉接器。

功能
本技能說明 @json-render/zustand 套件,它把 Zustand vanilla store 轉接成 json-render 的 StateStore 介面。內容涵蓋安裝方式、透過 StateProvider 接入 store 的基本用法範例,以及使用 selector 與 updater 選項處理巢狀切片的範例。也列出 zustandStateStore 的選項:store、selector 與 updater。
適用情境
當需要把 json-render 與 Zustand 整合,讓 json-render 的讀寫經由 Zustand store 進行時使用。也適合從較大的 store 中選取巢狀切片,或自訂新狀態的套用方式。
執行需求
需要 Zustand v5 或更新版本(不支援 v4),以及透過 npm 安裝 @json-render/zustand、@json-render/core、@json-render/react 與 zustand 等套件。不含指令碼,僅為說明文件。

@json-render/zustand

Zustand adapter for json-render's StateStore interface. Wire a Zustand vanilla store as the state backend for json-render.

Installation

bash
npm install @json-render/zustand @json-render/core @json-render/react zustand

Requires Zustand v5+. Zustand v4 is not supported due to breaking API changes in the vanilla store interface.

Usage

tsx
import { createStore } from "zustand/vanilla";import { zustandStateStore } from "@json-render/zustand";import { StateProvider } from "@json-render/react";
// 1. Create a Zustand vanilla storeconst bearStore = createStore(() => ({  count: 0,  name: "Bear",}));
// 2. Create the json-render StateStore adapterconst store = zustandStateStore({ store: bearStore });
// 3. Use it<StateProvider store={store}>  {/* json-render reads/writes go through Zustand */}</StateProvider>

With a Nested Slice

tsx
const appStore = createStore(() => ({  ui: { count: 0 },  auth: { token: null },}));
const store = zustandStateStore({  store: appStore,  selector: (s) => s.ui,  updater: (next, s) => s.setState({ ui: next }),});

API

zustandStateStore(options)

Creates a StateStore backed by a Zustand store.

OptionTypeRequiredDescription
storeStoreApi<S>YesZustand vanilla store (from createStore in zustand/vanilla)
selector(state) => StateModelNoSelect the json-render slice. Defaults to entire state.
updater(nextState, store) => voidNoApply next state to the store. Defaults to shallow merge. Override for nested slices, or use (next, s) => s.setState(next, true) for full replacement.

來源與署名

來源:vercel-labs/json-render位於skills/zustand提交fc2a696

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架