Added some external resources to readme
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 1139f57 · Browse files · 2 files·+43 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+43 added−4 removed
- tests:+18 added−0 removed
- other:+25 added−4 removed
Provenance
- Commit
c018c49- Tree
fca7ce9- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
1139f57
2 files changed+43 added−4 removed
modified:__tests__/curry.js+18 added
61 unmodified lines62636465666768697071727374757677787980818283848561 unmodified lines
expect(reducer(undefined, 3)).toEqual({hello: "world", index: 3}) expect(reducer({}, 3)).toEqual({index: 3}) })
it("can has fun with change detection", () => { const spread = produce(Object.assign)
const base = { x: 1, y: 1 }
expect({...base}).not.toBe(base) expect(spread(base, {})).toBe(base) expect(spread(base, {y: 1})).toBe(base) expect(spread(base, {...base})).toBe(base) expect(spread(base, {...base, y: 2})).not.toBe(base) expect(spread(base, {...base, y: 2})).toEqual({x: 1, y: 2}) expect(spread(base, {z: 3})).toEqual({x: 1, y: 1, z: 3}) expect(spread(base, {y: 1})).toBe(base) }) })}modified:readme.md+25 added-4 removed
18 unmodified lines19 ---20 21 * 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)22 * Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)23-* [Talk](https://www.youtube.com/watch?v=-gJbS7YjcSo) + [slides](http://immer.surge.sh/) on Immer at React Finland 2018 by Michel Weststrate24-* Blog: by Workday Prism on why they picked Immer to manage immutable state [The Search for a Strongly-Typed, Immutable State](https://medium.com/workday-engineering/workday-prism-analytics-the-search-for-a-strongly-typed-immutable-state-a09f6768b2b5)25-* Blog: [The Rise of Immer in React](https://www.netlify.com/blog/2018/09/12/the-rise-of-immer-in-react/)26-* Blog: [Immutability in React and Redux: The Complete Guide](https://daveceddia.com/react-redux-immutability-guide/)23 24 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.25 26 The basic idea is that you will apply all your changes to a temporarily _draftState_, which is a proxy of the _currentState_. Once all your mutations are completed, Immer will produce the _nextState_ based on the mutations to the draft state. This means that you can interact with your data by simply modifying it, while keeping all the benefits of immutable data.3 unmodified lines30 Using Immer is like having a personal assistant; he takes a letter (the current state), and gives you a copy (draft) to jot changes onto. Once you are done, the assistant will take your draft and produce the real immutable, final letter for you (the next state).31 32 A mindful reader might notice that this is quite similar to `withMutations` of ImmutableJS. It is indeed, but generalized and applied to plain, native JavaScript data structures (arrays and objects) without further needing any library.33 34+## External resources35+36+* Blog: [The Rise of Immer in React](https://www.netlify.com/blog/2018/09/12/the-rise-of-immer-in-react/)37+* Blog: by Workday Prism on why they picked Immer to manage immutable state [The Search for a Strongly-Typed, Immutable State](https://medium.com/workday-engineering/workday-prism-analytics-the-search-for-a-strongly-typed-immutable-state-a09f6768b2b5)38+* Blog: [Immutability in React and Redux: The Complete Guide](https://daveceddia.com/react-redux-immutability-guide/)39+* Video tutorial: [Using Immer with React.setState](https://codedaily.io/screencasts/86/Immutable-Data-with-Immer-and-React-setState)40+* [Talk](https://www.youtube.com/watch?v=-gJbS7YjcSo) + [slides](http://immer.surge.sh/) on Immer at React Finland 2018 by Michel Weststrate41+42 ## API43 44 The Immer package exposes a default function that does all the work.45 184 unmodified lines230 }231 )232 ```233 234+##### Fun with currying235+236+A random fun example just for inspiration:237+a neat trick is to turn `Object.assign` into a producer to create a "spread" function that is smarter than the normal spread operator, as it doesn't produce a new state if the result doesn't actually change ([details & explanation](https://twitter.com/mweststrate/status/1045059430256119809)).238+Quick example:239+240+```javascript241+import produce from "immer"242+const spread = produce(Object.assign)243+244+const base = {x: 1, y: 1}245+246+console.log({...base, y: 1} === base) // false247+console.log(spread(base, {y: 1}) === base) // true! base is recycled as no actual new value was produced248+console.log(spread(base, {y: 2}) === base) // false, produced a new object as it should249+```250+251 ## Patches252 253 During the run of a producer, Immer can record all the patches that would replay the changes made by the reducer.254 This is a very powerful tool if you want to fork your state temporarily, and replay the changes to the original.448 unmodified lines