Skip to content
Myr

introduction.md

This file

Path

website/docs/introduction.md

67 lines · 2.8 KiB · Markdown

Last changed by

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

History of this file

Take it

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 1–55why, one step back
1---
2id: introduction3title: Introduction to Immer4sidebar_label: Introduction5slug: /6---78<center>9<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>10</center>1112<img src="/immer/img/immer-logo.svg" height="200px" align="right"/>1314# Immer1516Winner 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.1718- Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)19- 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)20- Free in-depth Egghead.io course: [Immutable JavaScript Data Structures with Immer (58m)](https://egghead.io/courses/immutable-javascript-data-structures-with-immer)2122---2324Immer (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.2526The 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.2728![immer-hd.png](/img/immer.png)2930Using 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).3132## Quick Example3334```javascript35import produce from "immer"3637const baseState = [38 {39 todo: "Learn typescript",40 done: true41 },42 {43 todo: "Try immer",44 done: false45 }46]4748const nextState = produce(baseState, draftState => {49 draftState.push({todo: "Tweet about it"})50 draftState[1].done = true51})52```5354The 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).55

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

line 56why, one step back
56Head to the [next section](./produce.mdx) to further dive into `produce`.

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 57–67why, one step back
57
58## Benefits5960- Immutability with normal JavaScript objects, arrays, Sets and Maps. No new APIs to learn!61- Strongly typed, no string based paths selectors etc.62- Structural sharing out of the box63- Object freezing out of the box64- Deep updates are a breeze65- Boilerplate reduction. Less noise, more concise code.66- First class support for patches67- Small: 3KB gzipped