$npx -y skills add vercel-labs/json-render --skill reduxRedux adapter for json-render's StateStore interface. Use when integrating json-render with Redux or Redux Toolkit for state management via @json-render/redux.
| 1 | # @json-render/redux |
| 2 | |
| 3 | Redux adapter for json-render's `StateStore` interface. Wire a Redux store (or Redux Toolkit slice) as the state backend for json-render. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install @json-render/redux @json-render/core @json-render/react redux |
| 9 | # or with Redux Toolkit (recommended): |
| 10 | npm install @json-render/redux @json-render/core @json-render/react @reduxjs/toolkit |
| 11 | ``` |
| 12 | |
| 13 | ## Usage |
| 14 | |
| 15 | ```tsx |
| 16 | import { configureStore, createSlice } from "@reduxjs/toolkit"; |
| 17 | import { reduxStateStore } from "@json-render/redux"; |
| 18 | import { StateProvider } from "@json-render/react"; |
| 19 | |
| 20 | // 1. Define a slice for json-render state |
| 21 | const uiSlice = createSlice({ |
| 22 | name: "ui", |
| 23 | initialState: { count: 0 } as Record<string, unknown>, |
| 24 | reducers: { |
| 25 | replaceUiState: (_state, action) => action.payload, |
| 26 | }, |
| 27 | }); |
| 28 | |
| 29 | // 2. Create the Redux store |
| 30 | const reduxStore = configureStore({ |
| 31 | reducer: { ui: uiSlice.reducer }, |
| 32 | }); |
| 33 | |
| 34 | // 3. Create the json-render StateStore adapter |
| 35 | const store = reduxStateStore({ |
| 36 | store: reduxStore, |
| 37 | selector: (state) => state.ui, |
| 38 | dispatch: (next, s) => s.dispatch(uiSlice.actions.replaceUiState(next)), |
| 39 | }); |
| 40 | |
| 41 | // 4. Use it |
| 42 | <StateProvider store={store}> |
| 43 | {/* json-render reads/writes go through Redux */} |
| 44 | </StateProvider> |
| 45 | ``` |
| 46 | |
| 47 | ## API |
| 48 | |
| 49 | ### `reduxStateStore(options)` |
| 50 | |
| 51 | Creates a `StateStore` backed by a Redux store. |
| 52 | |
| 53 | | Option | Type | Required | Description | |
| 54 | |--------|------|----------|-------------| |
| 55 | | `store` | `Store` | Yes | The Redux store instance | |
| 56 | | `selector` | `(state) => StateModel` | Yes | Select the json-render slice from the Redux state tree. Use `(s) => s` if the entire state is the model. | |
| 57 | | `dispatch` | `(nextState, store) => void` | Yes | Dispatch an action that replaces the selected slice with the next state | |
| 58 | |
| 59 | The `dispatch` callback receives the full next state model and the Redux store. |