page.mdx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/api/jotai")
  3. # @json-render/jotai
  4. Jotai adapter for json-render's `StateStore` interface.
  5. ## Installation
  6. ```bash
  7. npm install @json-render/jotai @json-render/core @json-render/react jotai
  8. ```
  9. ## jotaiStateStore
  10. Create a `StateStore` backed by a Jotai atom.
  11. ```typescript
  12. import { jotaiStateStore } from "@json-render/jotai";
  13. ```
  14. ### Options
  15. <table>
  16. <thead>
  17. <tr>
  18. <th>Option</th>
  19. <th>Type</th>
  20. <th>Required</th>
  21. <th>Description</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. <tr>
  26. <td><code>atom</code></td>
  27. <td><code>{'WritableAtom<StateModel, [StateModel], void>'}</code></td>
  28. <td>Yes</td>
  29. <td>A writable atom holding the state model.</td>
  30. </tr>
  31. <tr>
  32. <td><code>store</code></td>
  33. <td>Jotai <code>Store</code></td>
  34. <td>No</td>
  35. <td>The Jotai store instance. Defaults to a new store created internally. Pass your own to share state with <code>{'<Provider>'}</code>.</td>
  36. </tr>
  37. </tbody>
  38. </table>
  39. ### Example
  40. ```typescript
  41. import { atom } from "jotai";
  42. import { jotaiStateStore } from "@json-render/jotai";
  43. import { StateProvider } from "@json-render/react";
  44. const uiAtom = atom<Record<string, unknown>>({ count: 0 });
  45. const store = jotaiStateStore({ atom: uiAtom });
  46. ```
  47. ```tsx
  48. <StateProvider store={store}>
  49. {/* json-render reads/writes go through Jotai */}
  50. </StateProvider>
  51. ```
  52. ### Shared Jotai Store
  53. If your app already uses a Jotai `<Provider>` with a custom store, pass it so both json-render and your components share the same state:
  54. ```typescript
  55. import { atom, createStore } from "jotai";
  56. import { Provider as JotaiProvider } from "jotai/react";
  57. import { jotaiStateStore } from "@json-render/jotai";
  58. import { StateProvider } from "@json-render/react";
  59. const jStore = createStore();
  60. const uiAtom = atom<Record<string, unknown>>({ count: 0 });
  61. const store = jotaiStateStore({ atom: uiAtom, store: jStore });
  62. ```
  63. ```tsx
  64. <JotaiProvider store={jStore}>
  65. <StateProvider store={store}>
  66. {/* Both json-render and useAtom() see the same state */}
  67. </StateProvider>
  68. </JotaiProvider>
  69. ```
  70. ## Re-exports
  71. <table>
  72. <thead>
  73. <tr>
  74. <th>Export</th>
  75. <th>Source</th>
  76. </tr>
  77. </thead>
  78. <tbody>
  79. <tr>
  80. <td><code>StateStore</code></td>
  81. <td><code>@json-render/core</code></td>
  82. </tr>
  83. </tbody>
  84. </table>