return.md
This file
Path
docs/return.md
103 lines · 3.7 KiB · Markdown
Last changed by
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–103why, one step back1---2id: return3title: Returning new data from producers4---56<div id="codefund"><!-- fallback content --></div>78It 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`.910It 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:1112```javascript13const userReducer = produce((draft, action) => {14 switch (action.type) {15 case "renameUser":16 // OK: we modify the current state17 draft.users[action.payload.id].name = action.payload.name18 return draft // same as just 'return'19 case "loadUsers":20 // OK: we return an entirely new state21 return action.payload22 case "adduser-1":23 // NOT OK: This doesn't do change the draft nor return a new state!24 // It doesn't modify the draft (it just redeclares it)25 // In fact, this just doesn't do anything at all26 draft = {users: [...draft.users, action.payload]}27 return28 case "adduser-2":29 // NOT OK: modifying draft *and* returning a new state30 draft.userCount += 131 return {users: [...draft.users, action.payload]}32 case "adduser-3":33 // OK: returning a new state. But, unnecessary complex and expensive34 return {35 userCount: draft.userCount + 1,36 users: [...draft.users, action.payload]37 }38 case "adduser-4":39 // OK: the immer way40 draft.userCount += 141 draft.users.push(action.payload)42 return43 }44})45```4647_Note: It is not possible to return `undefined` this way, as it is indistinguishable from *not* updating the draft! Read on..._4849## Producing `undefined` using `nothing`5051So, 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`:5253```javascript54produce({}, draft => {55 // don't do anything56})57```5859Versus:6061```javascript62produce({}, draft => {63 // Try to return undefined from the producer64 return undefined65})66```6768The 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.6970However, to make it clear to Immer that you intentionally want to produce the value `undefined`, you can return the built-in token `nothing`:7172```javascript73import produce, {nothing} from "immer"7475const state = {76 hello: "world"77}7879produce(state, draft => {})80produce(state, draft => undefined)81// Both return the original state: { hello: "world"}8283produce(state, draft => nothing)84// Produces a new state, 'undefined'85```8687N.B. Note that this problem is specific for the `undefined` value, any other value, including `null`, doesn't suffer from this issue.8889## Inline shortcuts using `void`9091Draft 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.9293In 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`.9495```javascript96// Single mutation97produce(draft => void (draft.user.age += 1))9899// Multiple mutations100produce(draft => void ((draft.user.age += 1), (draft.user.height = 186)))101```102103Code 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.