Added egghead.io video embeds
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 0349152 · Browse files · 11 files·+139 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+139 added−1 removed
- other:+139 added−1 removed
Provenance
- Commit
a866760- Tree
514d5ca- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
0349152
11 files changed+139 added−1 removed
modified:docs/async.md+9 added
4 unmodified lines5678910111213141516171819202133 unmodified lines4 unmodified lines---
<div id="codefund"><!-- fallback content --></div>
<details> <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 11: Creating _async_ producers (and why you shouldn’t)</summary> <br> <div style="padding:5px;"> <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t/embed" ></iframe> </div> <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t">Hosted on egghead.io</a></details>
It is allowed to return Promise objects from recipes. Or, in other words, to use `async / await`. This can be pretty useful for long running processes, that only produce the new object once the promise chain resolves. Note that `produce` itself (even in the curried form) will return a promise if the producer is async. Example:
```javascriptimport produce from "immer"33 unmodified linesmodified:docs/curried-produce.md+9 added
3 unmodified lines456789101112131415161718192049 unmodified lines3 unmodified lines---
<div id="codefund"><!-- fallback content --></div>
<details> <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 6: Simplify code by using curried _reduce_</summary> <br> <div style="padding:5px;"> <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/javascript-simplify-immer-producer-functions-using-currying/embed" ></iframe> </div> <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/javascript-simplify-immer-producer-functions-using-currying">Hosted on egghead.io</a></details>
Passing a function as the first argument to `produce` is intended to be used for currying. This means that you get a pre-bound producer that only needs a state to produce the value from. The producer function gets passed in the draft and any further arguments that were passed to the curried function.
For example:
49 unmodified linesmodified:docs/example-reducer.md+9 added
3 unmodified lines456789101112131415161718192037 unmodified lines3 unmodified lines---
<div id="codefund"><!-- fallback content --></div>
<details> <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 13: Using Immer in a (React) reducer</summary> <br> <div style="padding:5px;"> <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-update-immutable-state-with-react-usereducer-through-immer/embed" ></iframe> </div> <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-update-immutable-state-with-react-usereducer-through-immer">Hosted on egghead.io</a></details>
Here is a simple example of the difference that Immer could make in practice.
```javascript// Redux reducer37 unmodified linesmodified:docs/example-setstate.md+9 added
3 unmodified lines4 ---5 6 <div id="codefund"><!-- fallback content --></div>7 8+<details>9+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 8: Using Immer with _useState_. Or: _useImmer_</summary>10+ <br>11+ <div style="padding:5px;">12+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-immutable-update-state-inside-react-components-with-useimmer/embed" ></iframe>13+ </div>14+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-immutable-update-state-inside-react-components-with-useimmer">Hosted on egghead.io</a>15+</details>16+17 Deep updates in the state of React components can be greatly simplified as well by using immer. Take for example the following onClick handlers (Try in [codesandbox](https://codesandbox.io/s/m4yp57632j)):18 19 ```javascript20 /**23 unmodified linesmodified:docs/freezing.md+11 added
12345678910111213141516171819---id: freezingtitle: Auto freezing---
<div id="codefund"><!-- fallback content --></div>
<details> <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 7: Immer automatically freezes data</summary> <br> <div style="padding:5px;"> <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer/embed" ></iframe> </div> <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer">Hosted on egghead.io</a></details>
Immer automatically freezes any state trees that are modified using `produce`. This protects against accidental modifications of the state tree outside of a producer. This comes with a performance impact, so it is recommended to disable this option in production. By default, it is turned on during local development and turned off in production. Use `setAutoFreeze(true / false)` to explicitly turn this feature on or off.
_⚠️ If auto freezing is enabled, recipes are not entirely side-effect free: Any plain object or array that ends up in the produced result, will be frozen, even when these objects were not frozen before the start of the producer! ⚠️_modified:docs/introduction.md+2 added-1 removed
10 unmodified lines11 # Immer12 13 Winner of the "Breakthrough of the year" [React open source award](https://osawards.com/react/) and "Most impactful contribution" [JavaScript open source award](https://osawards.com/javascript/) in 2019.14 15-- Egghead lesson covering all of immer (7m): [Simplify creating immutable data trees with Immer](https://egghead.io/lessons/redux-simplify-creating-immutable-data-trees-with-immer)15 - Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)16+- Short Egghead.io lesson covering the Immer essentials: [Simplify creating immutable data trees with Immer (7m)](https://egghead.io/lessons/redux-simplify-creating-immutable-data-trees-with-immer)17+- Free in-depth Egghead.io course: [Immutable JavaScript Data Structures with Immer (58m)](https://egghead.io/courses/immutable-javascript-data-structures-with-immer)18 19 ---20 21 Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way. It is based on the [_copy-on-write_](https://en.wikipedia.org/wiki/Copy-on-write) mechanism.43 unmodified linesmodified:docs/patches.md+36 added
3 unmodified lines4 ---5 6 <div id="codefund"><!-- fallback content --></div>7 8+<details>9+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 14: Capture patches using _produceWithPatches_</summary>10+ <br>11+ <div style="padding:5px;">12+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-capture-patches-to-distribute-changes-in-app-state-with-immer-producewithpatches/embed" ></iframe>13+ </div>14+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-capture-patches-to-distribute-changes-in-app-state-with-immer-producewithpatches">Hosted on egghead.io</a>15+</details>16+17+<details>18+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 16: Apply Patches using _applyPatches_</summary>19+ <br>20+ <div style="padding:5px;">21+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-apply-patches-using-immer-applypatches-to-synchronize-state-across-clients/embed" ></iframe>22+ </div>23+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-apply-patches-using-immer-applypatches-to-synchronize-state-across-clients">Hosted on egghead.io</a>24+</details>25+26 During the run of a producer, Immer can record all the patches that would replay the changes made by the reducer. This is a very powerful tool if you want to fork your state temporarily and replay the changes to the original.27 28 Patches are useful in few scenarios:29 75 unmodified lines105 ```106 107 ### `produceWithPatches`108 109+<details>110+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 19: Using inverse patches to build undo functionality</summary>111+ <br>112+ <div style="padding:5px;">113+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-use-immer-inversepatches-to-build-undo-functionality/embed" ></iframe>114+ </div>115+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-use-immer-inversepatches-to-build-undo-functionality">Hosted on egghead.io</a>116+</details>117+118+<details>119+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 20: Use patches to build redo functionality</summary>120+ <br>121+ <div style="padding:5px;">122+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-use-immer-patches-to-build-redo-functionality/embed" ></iframe>123+ </div>124+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-use-immer-patches-to-build-redo-functionality">Hosted on egghead.io</a>125+</details>126+127 Instead of setting up a patch listener, an easier way to obtain the patches is to use `produceWithPatches`, which has the same signature as `produce`, except that it doesn't return just the next state, but a tuple consisting of `[nextState, patches, inversePatches]`. Like `produce`, `produceWithPatches` supports currying as well.128 129 ```javascript130 import {produceWithPatches} from "immer"38 unmodified linesmodified:docs/performance.md+18 added
3 unmodified lines4 ---5 6 <div id="codefund"><!-- fallback content --></div>7 8+<details>9+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 5: Leveraging Immer's structural sharing in React</summary>10+ <br>11+ <div style="padding:5px;">12+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-profile-react-rendering-and-optimize-with-memo-to-leverage-structural-sharing/embed" ></iframe>13+ </div>14+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-profile-react-rendering-and-optimize-with-memo-to-leverage-structural-sharing">Hosted on egghead.io</a>15+</details>16+17+<details>18+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 7: Immer will try to re-cycle data if there was no semantic change</summary>19+ <br>20+ <div style="padding:5px;">21+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer/embed" ></iframe>22+ </div>23+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer">Hosted on egghead.io</a>24+</details>25+26 Here is a [simple benchmark](https://github.com/immerjs/immer/blob/master/__performance_tests__/todo.js) on the performance of Immer. This test takes 50,000 todo items and updates 5,000 of them. _Freeze_ indicates that the state tree has been frozen after producing it. This is a _development_ best practice, as it prevents developers from accidentally modifying the state tree.27 28 Something that isn't reflected in the numbers above, but in reality, Immer is sometimes significantly _faster_ than a hand written reducer. The reason for that is that Immer will detect "no-op" state changes, and return the original state if nothing actually changed, which can avoid a lot of re-renderings for example. Cases are known where simply applying immer solved critical performance issues.29 10 unmodified linesmodified:docs/produce.md+9 added
3 unmodified lines4 ---5 6 <div id="codefund"><!-- fallback content --></div>7 8+<details>9+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 3: Simplifying deep updates with _produce_</summary>10+ <br>11+ <div style="padding:5px;">12+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/javascript-simplify-deep-state-updates-using-immer-produce/embed" ></iframe>13+ </div>14+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/javascript-simplify-deep-state-updates-using-immer-produce">Hosted on egghead.io</a>15+</details>16+17 The Immer package exposes a default function that does all the work.18 19 `produce(currentState, producer: (draftState) => void): nextState`20 41 unmodified linesmodified:docs/return.md+18 added
3 unmodified lines4 ---5 6 <div id="codefund"><!-- fallback content --></div>7 8+<details>9+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 9: Returning completely new state</summary>10+ <br>11+ <div style="padding:5px;">12+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-return-completely-new-state-from-an-immer-producer/embed" ></iframe>13+ </div>14+ <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>15+</details>16+17 It 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`.18 19 It 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:20 75 unmodified lines96 N.B. Note that this problem is specific for the `undefined` value, any other value, including `null`, doesn't suffer from this issue.97 98 ## Inline shortcuts using `void`99 100+<details>101+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 10: Avoid accidental returns by using _void_</summary>102+ <br>103+ <div style="padding:5px;">104+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-avoid-accidental-returns-of-new-state-by-using-the-void-keyword/embed" ></iframe>105+ </div>106+ <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>107+</details>108+109 Draft 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.110 111 In 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`.112 9 unmodified linesmodified:docs/typescript.md+9 added
4 unmodified lines5 ---6 7 <div id="codefund"><!-- fallback content --></div>8 9+<details>10+ <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 12: Immer + TypeScript</summary>11+ <br>12+ <div style="padding:5px;">13+ <iframe style="border: none;" width=760 height=427 src="https://egghead.io/lessons/react-type-immutable-immer-data-with-typescript/embed" ></iframe>14+ </div>15+ <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-type-immutable-immer-data-with-typescript">Hosted on egghead.io</a>16+</details>17+18 The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.19 20 The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:21 50 unmodified lines