Skip to content
Myr

introduction.md

This file

Path

docs/introduction.md

66 lines · 2.8 KiB · Markdown

Last changed by

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–6why, one step back
1---
2id: introduction3title: Introduction to Immer4sidebar_label: Introduction5---6

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

lines 7–9why, one step back
7<center>
8<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>9</center>

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 10why, one step back
10

Merge pull request #473 from slikts/masterMichel Weststratebefore adoptionc1432fa

line 11why, one step back
11<img src="/immer/img/immer-logo.svg" height="200px" align="right"/>

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 12–17why, one step back
12
13# Immer1415Winner 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.1617- Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)

Added egghead.io video embedsMichel Weststratebefore adoptiona866760

lines 18–19why, one step back
18- 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)
19- Free in-depth Egghead.io course: [Immutable JavaScript Data Structures with Immer (58m)](https://egghead.io/courses/immutable-javascript-data-structures-with-immer)

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 20–52why, one step back
20
21---2223Immer (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.2425The 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.2627![immer-hd.png](/immer/img/immer.png)2829Using 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).3031## Quick Example3233```javascript34import produce from "immer"3536const baseState = [37 {38 todo: "Learn typescript",39 done: true40 },41 {42 todo: "Try immer",43 done: false44 }45]4647const nextState = produce(baseState, draftState => {48 draftState.push({todo: "Tweet about it"})49 draftState[1].done = true50})51```52

Merge pull request #424 from ravanscafi/patch-1Michel Weststratebefore adoption154c0bc

line 53why, one step back
53The 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 54–58why, one step back
54
55Head to the [next section](produce) to further dive into `produce`.5657## Benefits58

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 59why, one step back
59- Immutability with normal JavaScript objects, arrays, Sets and Maps. No new APIs to learn!

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 60–65why, one step back
60- Strongly typed, no string based paths selectors etc.
61- Structural sharing out of the box62- Object freezing out of the box63- Deep updates are a breeze64- Boilerplate reduction. Less noise, more concise code.65- First class support for patches

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 66why, one step back
66- Small: 3KB gzipped