introduction.md
This file
Path
docs/introduction.md
63 lines · 2.7 KiB · Markdown
Last changed by
Merge pull request #424 from ravanscafi/patch-1Michel Weststratebefore adoption154c0bc
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–49why, one step back1---2id: introduction3title: Introduction to Immer4sidebar_label: Introduction5---67<div id="codefund"><!-- fallback content --></div>89<img src="/immer/img/immer-logo.png" height="200px" align="right"/>1011# Immer1213Winner 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.1415- 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)16- Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)1718---1920Immer (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.2122The basic idea is that you will apply all your changes to a temporary _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.23242526Using 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).2728## Quick Example2930```javascript31import produce from "immer"3233const baseState = [34 {35 todo: "Learn typescript",36 done: true37 },38 {39 todo: "Try immer",40 done: false41 }42]4344const nextState = produce(baseState, draftState => {45 draftState.push({todo: "Tweet about it"})46 draftState[1].done = true47})48```49Merge pull request #424 from ravanscafi/patch-1Michel Weststratebefore adoption154c0bc
line 50why, one step back50The interesting thing about Immer is that the `baseState` will be untouched, but the `nextState` will be a new immutable tree that reflects all changes made to `draftState` (and structurally sharing the things that weren't changed).Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 51–63why, one step back5152Head to the [next section](produce) to further dive into `produce`.5354## Benefits5556- Immutability with normal JavaScript objects and arrays. No new APIs to learn!57- Strongly typed, no string based paths selectors etc.58- Structural sharing out of the box59- Object freezing out of the box60- Deep updates are a breeze61- Boilerplate reduction. Less noise, more concise code.62- First class support for patches63- Small [](https://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js)