page.mdx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/api/zustand")
  3. # @json-render/zustand
  4. Zustand adapter for json-render's `StateStore` interface.
  5. Requires Zustand v5+. Zustand v4 is not supported due to breaking API changes in the vanilla store interface.
  6. ## Installation
  7. ```bash
  8. npm install @json-render/zustand @json-render/core @json-render/react zustand
  9. ```
  10. ## zustandStateStore
  11. Create a `StateStore` backed by a Zustand vanilla store.
  12. ```typescript
  13. import { zustandStateStore } from "@json-render/zustand";
  14. ```
  15. ### Options
  16. <table>
  17. <thead>
  18. <tr>
  19. <th>Option</th>
  20. <th>Type</th>
  21. <th>Required</th>
  22. <th>Description</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <tr>
  27. <td><code>store</code></td>
  28. <td><code>{'StoreApi<S>'}</code></td>
  29. <td>Yes</td>
  30. <td>A Zustand vanilla store (from <code>createStore</code> in <code>zustand/vanilla</code>).</td>
  31. </tr>
  32. <tr>
  33. <td><code>selector</code></td>
  34. <td><code>{'(state: S) => StateModel'}</code></td>
  35. <td>No</td>
  36. <td>Select the json-render slice from the store state. Defaults to the entire state.</td>
  37. </tr>
  38. <tr>
  39. <td><code>updater</code></td>
  40. <td><code>{'(nextState: StateModel, store: StoreApi<S>) => void'}</code></td>
  41. <td>No</td>
  42. <td>Apply a state change back to the store. Defaults to a shallow merge.</td>
  43. </tr>
  44. </tbody>
  45. </table>
  46. ### Example
  47. ```typescript
  48. import { createStore } from "zustand/vanilla";
  49. import { zustandStateStore } from "@json-render/zustand";
  50. import { StateProvider } from "@json-render/react";
  51. const bearStore = createStore(() => ({
  52. count: 0,
  53. name: "Bear",
  54. }));
  55. const store = zustandStateStore({ store: bearStore });
  56. ```
  57. ```tsx
  58. <StateProvider store={store}>
  59. {/* json-render reads/writes go through Zustand */}
  60. </StateProvider>
  61. ```
  62. ### Nested Slice
  63. ```typescript
  64. const appStore = createStore(() => ({
  65. ui: { count: 0 },
  66. auth: { token: null },
  67. }));
  68. const store = zustandStateStore({
  69. store: appStore,
  70. selector: (s) => s.ui,
  71. updater: (next, s) => s.setState({ ui: next }),
  72. });
  73. ```
  74. ## Re-exports
  75. <table>
  76. <thead>
  77. <tr>
  78. <th>Export</th>
  79. <th>Source</th>
  80. </tr>
  81. </thead>
  82. <tbody>
  83. <tr>
  84. <td><code>StateStore</code></td>
  85. <td><code>@json-render/core</code></td>
  86. </tr>
  87. </tbody>
  88. </table>