Skip to content
Myr

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 file

Take it

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 1–129why, one step back
1---
2title: Immer middleware3nav: 354---56The [Immer](https://github.com/immerjs/immer) middleware enables you7to use immutable state in a more convenient way.8Also, with Immer, you can simplify handling9immutable data structures in Zustand.1011## Installation1213In order to use the Immer middleware in Zustand,14you will need to install Immer as a direct dependency.1516```bash17npm install immer18```1920## Usage2122(Notice the extra parentheses after the type parameter as mentioned in the [Advanced Typescript Guide](../../learn/guides/advanced-typescript.md)).2324Updating simple states2526```ts27import { create } from 'zustand'28import { immer } from 'zustand/middleware/immer'2930type State = {31 count: number32}3334type Actions = {35 increment: (qty: number) => void36 decrement: (qty: number) => void37}3839export const useCountStore = create<State & Actions>()(40 immer((set) => ({41 count: 0,42 increment: (qty: number) =>43 set((state) => {44 state.count += qty45 }),46 decrement: (qty: number) =>47 set((state) => {48 state.count -= qty49 }),50 })),51)52```5354Updating complex states5556```ts57import { create } from 'zustand'58import { immer } from 'zustand/middleware/immer'5960interface Todo {61 id: string62 title: string63 done: boolean64}6566type State = {67 todos: Record<string, Todo>68}6970type Actions = {71 toggleTodo: (todoId: string) => void72}7374export const useTodoStore = create<State & Actions>()(75 immer((set) => ({76 todos: {77 '82471c5f-4207-4b1d-abcb-b98547e01a3e': {78 id: '82471c5f-4207-4b1d-abcb-b98547e01a3e',79 title: 'Learn Zustand',80 done: false,81 },82 '354ee16c-bfdd-44d3-afa9-e93679bda367': {83 id: '354ee16c-bfdd-44d3-afa9-e93679bda367',84 title: 'Learn Jotai',85 done: false,86 },87 '771c85c5-46ea-4a11-8fed-36cc2c7be344': {88 id: '771c85c5-46ea-4a11-8fed-36cc2c7be344',89 title: 'Learn Valtio',90 done: false,91 },92 '363a4bac-083f-47f7-a0a2-aeeee153a99c': {93 id: '363a4bac-083f-47f7-a0a2-aeeee153a99c',94 title: 'Learn Signals',95 done: false,96 },97 },98 toggleTodo: (todoId: string) =>99 set((state) => {100 state.todos[todoId].done = !state.todos[todoId].done101 }),102 })),103)104```105106## Gotchas107108In this section you will find some things109that you need to keep in mind when using Zustand with Immer.110111### My subscriptions aren't being called112113If you are using Immer,114make sure you are actually following115[the rules of Immer](https://immerjs.github.io/immer/pitfalls).116117For example, you have to add `[immerable] = true` for118[class objects](https://immerjs.github.io/immer/complex-objects) to work.119If you don't do this, Immer will still mutate the object,120but not as a proxy, so it will also update the current state.121Zustand checks if the state has actually changed,122so since both the current state and the next state are123equal (if you don't do it correctly),124Zustand will skip calling the subscriptions.125126## Demos127128- Basic: https://stackblitz.com/edit/vitejs-vite-3sgc4ejy129- Advanced: https://stackblitz.com/edit/vitejs-vite-jxxtuyj3