Skip to content

Commit e889967

Browse files
committed
Discourage use of createStoreWithProducer(…)
1 parent df0c0a2 commit e889967

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

docs/xstate-store.mdx

+39-3
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,48 @@ store.inspect(inspector);
302302

303303
## Using Immer
304304

305-
If you want to use [Immer](https://immerjs.github.io/immer/) to update the `context`, you can do so by passing in the `produce` function as the first argument to `createStoreWithProducer(producer, …)`.
305+
You can use the `produce(…)` function from [Immer](https://immerjs.github.io/immer/) to update the `context` in transitions:
306306

307307
```ts
308-
import { createStoreWithProducer } from '@xstate/store';
308+
import { createStore } from '@xstate/store';
309+
import { produce } from 'immer';
310+
311+
const store = createStore({
312+
context: { count: 0, todos: [] },
313+
on: {
314+
inc: (context, event: { by: number }) =>
315+
produce(context, (draft) => {
316+
draft.count += event.by;
317+
}),
318+
addTodo: (context, event: { todo: string }) =>
319+
produce(context, (draft) => {
320+
draft.todos.push(event.todo);
321+
}),
322+
// Not using a producer
323+
resetCount: (context) => ({
324+
...context,
325+
count: 0,
326+
}),
327+
},
328+
});
329+
```
330+
331+
<details>
332+
<summary>Deprecated: <code>createStoreWithProducer(…)</code></summary>
333+
334+
:::warning
335+
336+
This API is deprecated. Use `produce` from [Immer](https://immerjs.github.io/immer/) or similar libraries directly with `createStore(…)` instead.
337+
338+
:::
339+
340+
In previous versions of `@xstate/store`, you could use the `createStoreWithProducer(…)` function to pass in a producer function to update the `context` for every transition. This will not be supported in future versions of `@xstate/store`. Instead, you can use the `produce(…)` function from [Immer](https://immerjs.github.io/immer/) or similar libraries directly with `createStore(…)`.
341+
342+
```ts
343+
import { createStore } from '@xstate/store';
309344
import { produce } from 'immer';
310345

346+
// Deprecated API
311347
const store = createStoreWithProducer(produce, {
312348
context: { count: 0, todos: [] },
313349
on: {
@@ -325,7 +361,7 @@ const store = createStoreWithProducer(produce, {
325361
// ...
326362
```
327363

328-
Note that you cannot use the object assigner syntax when using `createStoreFromProducer(…)`, nor is it even necessary.
364+
</details>
329365

330366
## Usage with React
331367

0 commit comments

Comments
 (0)