introduction.md
This file
Path
website/docs/introduction.md
108 lines · 5.9 KiB · Markdown
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
The walk stopped after 200 commits: lines older than 6555173 point at it.
fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 1–8why, one step back1---2id: introduction3title: Introduction to Immer4sidebar_label: Introduction5slug: /6---78<center>chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3
line 9why, one step back9<div data-ea-publisher="immerjs" data-ea-type="image" className="horizontal bordered"></div>fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 10–11why, one step back10</center>11chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3
line 12why, one step back12<img src="/immer/img/immer-logo.svg" style={{maxHeight:200}} align="right"/>fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 13–15why, one step back1314# Immer15docs: added Mark Erikson quoteMichel Weststratebefore adoptione28b53f
lines 16–19why, one step back16Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way.1718> Immer is life-changing as a JS dev, and I'm not even exaggerating :) Like, it's right up there with Prettier in terms of "wow this package is amazing, how did I ever live without it?" --Mark Erikson, (the) Redux Maintainer, @replayio19fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 20–21why, one step back20Winner 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.21docs: added Mark Erikson quoteMichel Weststratebefore adoptione28b53f
lines 22–23why, one step back22---23fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 24–29why, one step back24- Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)25- 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)26- Free in-depth Egghead.io course: [Immutable JavaScript Data Structures with Immer (58m)](https://egghead.io/courses/immutable-javascript-data-structures-with-immer)2728---29docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 30why, one step back30### Immer simplifies handling immutable data structuresfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 31why, one step back31docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 32why, one step back32Immer can be used in any context in which immutable data structures need to be used. For example in combination with React state, React or Redux reducers, or configuration management. Immutable data structures allow for (efficient) change detection: if the reference to an object didn't change, the object itself did not change. In addition, it makes cloning relatively cheap: Unchanged parts of a data tree don't need to be copied and are shared in memory with older versions of the same state.fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 33why, one step back33docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 34why, one step back34Generally speaking, these benefits can be achieved by making sure you never change any property of an object, array or map, but by always creating an altered copy instead. In practice this can result in code that is quite cumbersome to write, and it is easy to accidentally violate those constraints. Immer will help you to follow the immutable data paradigm by addressing these pain points:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 35why, one step back35docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
lines 36–37why, one step back361. Immer will detect accidental mutations and throw an error.372. Immer will remove the need for the typical boilerplate code that is needed when creating deep updates to immutable objects: Without Immer, object copies need to be made by hand at every level. Typically by using a lot of `...` spread operations. When using Immer, changes are made to a `draft` object, that records the changes and takes care of creating the necessary copies, without ever affecting the original object.docs: fix adjective (#844)Blazej Sewerabefore adoptionfa3e6f4
line 38why, one step back383. When using Immer, you don't need to learn dedicated APIs or data structures to benefit from the paradigm. With Immer you'll use plain JavaScript data structures, and use the well-known mutable JavaScript APIs, but safely.fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 39why, one step back39docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 40why, one step back40### A quick example for comparisonfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 41why, one step back41docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 42why, one step back42```javascriptfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 43–44why, one step back43const baseState = [44 {docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 45why, one step back45 title: "Learn TypeScript",fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 46–48why, one step back46 done: true47 },48 {docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 49why, one step back49 title: "Try Immer",fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 50–52why, one step back50 done: false51 }52]docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 53why, one step back53```fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 54why, one step back54docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
lines 55–78why, one step back55Imagine we have the above base state, and we'll need to update the second todo, and add a third one. However, we don't want to mutate the original `baseState`, and we want to avoid deep cloning as well (to preserve the first todo).5657#### Without Immer5859Without Immer, we'll have to carefully shallow copy every level of the state structure that is affected by our change:6061```javascript62const nextState = baseState.slice() // shallow clone the array63nextState[1] = {64 // replace element 1...65 ...nextState[1], // with a shallow clone of element 166 done: true // ...combined with the desired update67}68// since nextState was freshly cloned, using push is safe here,69// but doing the same thing at any arbitrary time in the future would70// violate the immutability principles and introduce a bug!71nextState.push({title: "Tweet about it"})72```7374#### With Immer7576With Immer, this process is more straightforward. We can leverage the `produce` function, which takes as first argument the state we want to start from, and as second argument we pass a function, called the _recipe_, that is passed a `draft` to which we can apply straightforward mutations. Those mutations are recorded and used to produce the next state once the recipe is done. `produce` will take care of all the necessary copying, and protect against future accidental modifications as well by freezing the data.7778```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 79why, one step back79import {produce} from "immer"docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
lines 80–83why, one step back8081const nextState = produce(baseState, draft => {82 draft[1].done = true83 draft.push({title: "Tweet about it"})fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 84–86why, one step back84})85```86docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 87why, one step back87Looking for Immer in combination with React? Feel free to skip ahead to the [React + Immer](example-setstate) page.fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 88why, one step back88docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
lines 89–96why, one step back89### How Immer works9091The basic idea is that with Immer you will apply all your changes to a temporary _draft_, 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.92939495Using Immer is like having a personal assistant. The assistant 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).96fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 97–100why, one step back97Head to the [next section](./produce.mdx) to further dive into `produce`.9899## Benefits100docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 101why, one step back101- Follow the immutable data paradigm, while using normal JavaScript objects, arrays, Sets and Maps. No new APIs or "mutation patterns" to learn!fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 102–106why, one step back102- Strongly typed, no string based paths selectors etc.103- Structural sharing out of the box104- Object freezing out of the box105- Deep updates are a breeze106- Boilerplate reduction. Less noise, more concise code.docs: Extended / updated documenation (#824)Michel Weststratebefore adoption847492c
line 107why, one step back107- First class support for JSON patchesfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 108why, one step back108- Small: 3KB gzipped