curried-produce.mdx
This file
Path
website/docs/curried-produce.mdx
80 lines · 2.3 KiB
Last changed by
chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
History of this fileTake it
The walk stopped after 200 commits: lines older than 07f5cc9 point at it.
chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 1–11why, one step back1---2id: curried-produce3title: Curried producers4---56<center>7 <div8 data-ea-publisher="immerjs"9 data-ea-type="image"10 className="horizontal bordered"11 ></div>chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 12–13why, one step back12</center>13<details>chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 14–36why, one step back14 <summary className="egghead-summary">15 egghead.io lesson 6: Simplify code by using curried _reduce_16 </summary>17 <br />18 <div>19 <iframe20 width="760"21 height="427"22 scrolling="no"23 src="https://egghead.io/lessons/javascript-simplify-immer-producer-functions-using-currying/embed"24 ></iframe>25 </div>26 <a27 className="egghead-link"28 href="https://egghead.io/lessons/javascript-simplify-immer-producer-functions-using-currying"29 >30 Hosted on egghead.io31 </a>32</details>3334Passing a function as the first argument to `produce` creates a function that doesn't apply `produce` yet to a specific state, but rather creates a function that will apply `produce` to any state that is passed to it in the future. This generally is called _currying_. Take for example the following example:3536```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 37why, one step back37import {produce} from "immer"chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 38–64why, one step back3839function toggleTodo(state, id) {40 return produce(state, draft => {41 const todo = draft.find(todo => todo.id === id)42 todo.done = !todo.done43 })44}4546const baseState = [47 {48 id: "JavaScript",49 title: "Learn TypeScript",50 done: true51 },52 {53 id: "Immer",54 title: "Try Immer",55 done: false56 }57]5859const nextState = toggleTodo(baseState, "Immer")60```6162The above pattern of `toggleTodo` is quite typical; pass an existing state to `produce`, modify the `draft`, and then return the result. Since `state` isn't used for anything else than passing it on to `produce`, the above example can be simplified by using the _curried_ form of `produce`, where you pass `produce` only the recipe function, and `produce` will return a new function that will apply recipe to the base state. This allows us to shorten the above `toggleTodo` definition.6364```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 65why, one step back65import {produce} from "immer"chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 66–80why, one step back6667// curried producer:68const toggleTodo = produce((draft, id) => {69 const todo = draft.find(todo => todo.id === id)70 todo.done = !todo.done71})7273const baseState = [74 /* as is */75]7677const nextState = toggleTodo(baseState, "Immer")78```7980Note that the `id` param has now become part of the recipe function! This pattern of having curried producers combines really neatly with for example the `useState` hook from React, as we will see on the next page.