return.md
This file
Path
docs/return.md
123 lines · 4.9 KiB · Markdown
Last changed by
docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–5why, one step back1---2id: return3title: Returning new data from producers4---5docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 6–8why, one step back6<center>7<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>8</center>Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
line 9why, one step back9Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 10–13why, one step back10<details>11 <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 9: Returning completely new state</summary>12 <br>13 <div style="padding:5px;">docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 14why, one step back14 <iframe style="border: none;" width=760 height=427 scrolling="no" src="https://egghead.io/lessons/react-return-completely-new-state-from-an-immer-producer/embed" ></iframe>Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 15–18why, one step back15 </div>16 <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-return-completely-new-state-from-an-immer-producer">Hosted on egghead.io</a>17</details>18Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 19–100why, one step back19It is not needed to return anything from a producer, as Immer will return the (finalized) version of the `draft` anyway. However, it is allowed to just `return draft`.2021It is also allowed to return arbitrarily other data from the producer function. But _only_ if you didn't modify the draft. This can be useful to produce an entirely new state. Some examples:2223```javascript24const userReducer = produce((draft, action) => {25 switch (action.type) {26 case "renameUser":27 // OK: we modify the current state28 draft.users[action.payload.id].name = action.payload.name29 return draft // same as just 'return'30 case "loadUsers":31 // OK: we return an entirely new state32 return action.payload33 case "adduser-1":34 // NOT OK: This doesn't do change the draft nor return a new state!35 // It doesn't modify the draft (it just redeclares it)36 // In fact, this just doesn't do anything at all37 draft = {users: [...draft.users, action.payload]}38 return39 case "adduser-2":40 // NOT OK: modifying draft *and* returning a new state41 draft.userCount += 142 return {users: [...draft.users, action.payload]}43 case "adduser-3":44 // OK: returning a new state. But, unnecessary complex and expensive45 return {46 userCount: draft.userCount + 1,47 users: [...draft.users, action.payload]48 }49 case "adduser-4":50 // OK: the immer way51 draft.userCount += 152 draft.users.push(action.payload)53 return54 }55})56```5758_Note: It is not possible to return `undefined` this way, as it is indistinguishable from *not* updating the draft! Read on..._5960## Producing `undefined` using `nothing`6162So, in general, one can replace the current state by just `return`ing a new value from the producer, rather than modifying the draft. There is a subtle edge case however: if you try to write a producer that wants to replace the current state with `undefined`:6364```javascript65produce({}, draft => {66 // don't do anything67})68```6970Versus:7172```javascript73produce({}, draft => {74 // Try to return undefined from the producer75 return undefined76})77```7879The problem is that in JavaScript a function that doesn't return anything also returns `undefined`! So immer cannot differentiate between those different cases. So, by default, Immer will assume that any producer that returns `undefined` just tried to modify the draft.8081However, to make it clear to Immer that you intentionally want to produce the value `undefined`, you can return the built-in token `nothing`:8283```javascript84import produce, {nothing} from "immer"8586const state = {87 hello: "world"88}8990produce(state, draft => {})91produce(state, draft => undefined)92// Both return the original state: { hello: "world"}9394produce(state, draft => nothing)95// Produces a new state, 'undefined'96```9798N.B. Note that this problem is specific for the `undefined` value, any other value, including `null`, doesn't suffer from this issue.99100## Inline shortcuts using `void`Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 101–105why, one step back101102<details>103 <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 10: Avoid accidental returns by using _void_</summary>104 <br>105 <div style="padding:5px;">docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 106why, one step back106 <iframe style="border: none;" width=760 height=427 scrolling="no" src="https://egghead.io/lessons/react-avoid-accidental-returns-of-new-state-by-using-the-void-keyword/embed" ></iframe>Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 107–109why, one step back107 </div>108 <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-avoid-accidental-returns-of-new-state-by-using-the-void-keyword">Hosted on egghead.io</a>109</details>Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 110–123why, one step back110111Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with.112113In such cases, you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.114115```javascript116// Single mutation117produce(draft => void (draft.user.age += 1))118119// Multiple mutations120produce(draft => void ((draft.user.age += 1), (draft.user.height = 186)))121```122123Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft => { draft.user.age += 1}` to avoid cognitive overhead.