$npx -y skills add vercel-labs/json-render --skill zustandZustand adapter for json-render's StateStore interface. Use when integrating json-render with Zustand for state management via @json-render/zustand.
| 1 | # @json-render/zustand |
| 2 | |
| 3 | Zustand adapter for json-render's `StateStore` interface. Wire a Zustand vanilla store as the state backend for json-render. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install @json-render/zustand @json-render/core @json-render/react zustand |
| 9 | ``` |
| 10 | |
| 11 | Requires Zustand v5+. Zustand v4 is not supported due to breaking API changes in the vanilla store interface. |
| 12 | |
| 13 | ## Usage |
| 14 | |
| 15 | ```tsx |
| 16 | import { createStore } from "zustand/vanilla"; |
| 17 | import { zustandStateStore } from "@json-render/zustand"; |
| 18 | import { StateProvider } from "@json-render/react"; |
| 19 | |
| 20 | // 1. Create a Zustand vanilla store |
| 21 | const bearStore = createStore(() => ({ |
| 22 | count: 0, |
| 23 | name: "Bear", |
| 24 | })); |
| 25 | |
| 26 | // 2. Create the json-render StateStore adapter |
| 27 | const store = zustandStateStore({ store: bearStore }); |
| 28 | |
| 29 | // 3. Use it |
| 30 | <StateProvider store={store}> |
| 31 | {/* json-render reads/writes go through Zustand */} |
| 32 | </StateProvider> |
| 33 | ``` |
| 34 | |
| 35 | ### With a Nested Slice |
| 36 | |
| 37 | ```tsx |
| 38 | const appStore = createStore(() => ({ |
| 39 | ui: { count: 0 }, |
| 40 | auth: { token: null }, |
| 41 | })); |
| 42 | |
| 43 | const store = zustandStateStore({ |
| 44 | store: appStore, |
| 45 | selector: (s) => s.ui, |
| 46 | updater: (next, s) => s.setState({ ui: next }), |
| 47 | }); |
| 48 | ``` |
| 49 | |
| 50 | ## API |
| 51 | |
| 52 | ### `zustandStateStore(options)` |
| 53 | |
| 54 | Creates a `StateStore` backed by a Zustand store. |
| 55 | |
| 56 | | Option | Type | Required | Description | |
| 57 | |--------|------|----------|-------------| |
| 58 | | `store` | `StoreApi<S>` | Yes | Zustand vanilla store (from `createStore` in `zustand/vanilla`) | |
| 59 | | `selector` | `(state) => StateModel` | No | Select the json-render slice. Defaults to entire state. | |
| 60 | | `updater` | `(nextState, store) => void` | No | Apply next state to the store. Defaults to shallow merge. Override for nested slices, or use `(next, s) => s.setState(next, true)` for full replacement. | |