immer-middleware.md
This file
Path
docs/reference/integrations/immer-middleware.md
129 lines · 3.0 KiB · Markdown
Last changed by
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130---title: Immer middlewarenav: 35---
The [Immer](https://github.com/immerjs/immer) middleware enables youto use immutable state in a more convenient way.Also, with Immer, you can simplify handlingimmutable data structures in Zustand.
## Installation
In order to use the Immer middleware in Zustand,you will need to install Immer as a direct dependency.
```bashnpm install immer```
## Usage
(Notice the extra parentheses after the type parameter as mentioned in the [Advanced Typescript Guide](../../learn/guides/advanced-typescript.md)).
Updating simple states
```tsimport { create } from 'zustand'import { immer } from 'zustand/middleware/immer'
type State = { count: number}
type Actions = { increment: (qty: number) => void decrement: (qty: number) => void}
export const useCountStore = create<State & Actions>()( immer((set) => ({ count: 0, increment: (qty: number) => set((state) => { state.count += qty }), decrement: (qty: number) => set((state) => { state.count -= qty }), })),)```
Updating complex states
```tsimport { create } from 'zustand'import { immer } from 'zustand/middleware/immer'
interface Todo { id: string title: string done: boolean}
type State = { todos: Record<string, Todo>}
type Actions = { toggleTodo: (todoId: string) => void}
export const useTodoStore = create<State & Actions>()( immer((set) => ({ todos: { '82471c5f-4207-4b1d-abcb-b98547e01a3e': { id: '82471c5f-4207-4b1d-abcb-b98547e01a3e', title: 'Learn Zustand', done: false, }, '354ee16c-bfdd-44d3-afa9-e93679bda367': { id: '354ee16c-bfdd-44d3-afa9-e93679bda367', title: 'Learn Jotai', done: false, }, '771c85c5-46ea-4a11-8fed-36cc2c7be344': { id: '771c85c5-46ea-4a11-8fed-36cc2c7be344', title: 'Learn Valtio', done: false, }, '363a4bac-083f-47f7-a0a2-aeeee153a99c': { id: '363a4bac-083f-47f7-a0a2-aeeee153a99c', title: 'Learn Signals', done: false, }, }, toggleTodo: (todoId: string) => set((state) => { state.todos[todoId].done = !state.todos[todoId].done }), })),)```
## Gotchas
In this section you will find some thingsthat you need to keep in mind when using Zustand with Immer.
### My subscriptions aren't being called
If you are using Immer,make sure you are actually following[the rules of Immer](https://immerjs.github.io/immer/pitfalls).
For example, you have to add `[immerable] = true` for[class objects](https://immerjs.github.io/immer/complex-objects) to work.If you don't do this, Immer will still mutate the object,but not as a proxy, so it will also update the current state.Zustand checks if the state has actually changed,so since both the current state and the next state areequal (if you don't do it correctly),Zustand will skip calling the subscriptions.
## Demos
- Basic: https://stackblitz.com/edit/vitejs-vite-3sgc4ejy- Advanced: https://stackblitz.com/edit/vitejs-vite-jxxtuyj3