produce.mdx
This file
Path
website/docs/produce.mdx
90 lines · 3.0 KiB
Last changed by
docs: Update produce/mdx (#926)V Kbefore adoptionfcb7a55
History of this fileTake it
docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 1–11why, one step back1---2id: produce3title: Using produce4---56<center>7 <div8 data-ea-publisher="immerjs"9 data-ea-type="image"10 className="horizontal bordered"11 ></div>docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 12why, one step back12</center> <details>docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 13–34why, one step back13 <summary className="egghead-summary">14 egghead.io lesson 3: Simplifying deep updates with _produce_15 </summary>16 <br />17 <div>18 <iframe19 width="760"20 height="427"21 scrolling="no"22 src="https://egghead.io/lessons/javascript-simplify-deep-state-updates-using-immer-produce/embed"23 ></iframe>24 </div>25 <a26 className="egghead-link"27 href="https://egghead.io/lessons/javascript-simplify-deep-state-updates-using-immer-produce"28 >29 Hosted on egghead.io30 </a>31</details>3233The Immer package exposes a default function that does all the work.34docs: Update produce/mdx (#926)V Kbefore adoptionfcb7a55
line 35why, one step back35`produce(baseState, recipe: (draftState) => void): nextState`docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
line 36why, one step back36docs: Update produce.mdx (#868)Zac Ballbefore adoptionfb28e88
line 37why, one step back37`produce` takes a base state, and a _recipe_ that can be used to perform all the desired mutations on the `draft` that is passed in. The interesting thing about Immer is that the `baseState` will be untouched, but the `nextState` will reflect all changes made to `draftState`.docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
line 38why, one step back38docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
lines 39–42why, one step back39Inside the recipe, all standard JavaScript APIs can be used on the `draft` object, including field assignments, `delete` operations, and mutating array, Map and Set operations like `push`, `pop`, `splice`, `set`, `sort`, `remove`, etc.4041Any of those mutations don't have to happen at the root, but it is allowed to modify anything anywhere deep inside the draft: `draft.todos[0].tags["urgent"].author.age = 56`42docs: Fix link to `return` on `produce` doc page (#927)Peter Zichbefore adoption0a47a40
line 43why, one step back43Note that the recipe function itself normally doesn't return anything. However, it is possible to return in case you want to replace the `draft` object in its entirety with another object, for more details see [returning new data](./return.mdx).docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 44why, one step back44docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 45–51why, one step back45## Example4647```javascript48import produce from "immer"4950const baseState = [51 {docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 52why, one step back52 title: "Learn TypeScript",docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 53–55why, one step back53 done: true54 },55 {docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 56why, one step back56 title: "Try Immer",docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 57–61why, one step back57 done: false58 }59]6061const nextState = produce(baseState, draftState => {docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 62why, one step back62 draftState.push({title: "Tweet about it"})docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 63–78why, one step back63 draftState[1].done = true64})65```6667```javascript68// the new item is only added to the next state,69// base state is unmodified70expect(baseState.length).toBe(2)71expect(nextState.length).toBe(3)7273// same for the changed 'done' prop74expect(baseState[1].done).toBe(false)75expect(nextState[1].done).toBe(true)7677// unchanged data is structurally shared78expect(nextState[0]).toBe(baseState[0])docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 79why, one step back79// ...but changed data isn't.docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 80–81why, one step back80expect(nextState[1]).not.toBe(baseState[1])81```docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
lines 82–90why, one step back8283### Terminology8485- `(base)state`, the immutable state passed to `produce`86- `recipe`: the second argument of `produce`, that captures how the base state should be "mutated".87- `draft`: the first argument of any `recipe`, which is a proxy to the original base state that can be safely mutated.88- `producer`. A function that uses `produce` and is generally of the form `(baseState, ...arguments) => resultState`8990Note that it isn't strictly necessary to name the first argument of the recipe `draft`. You can name it anything you want, for example `users`. Using `draft` as a name is just a convention to signal: "mutation is OK here".