Skip to content
Myr

complex-objects.md

This file

Path

docs/complex-objects.md

78 lines · 2.5 KiB · Markdown

Last changed by

Updated docsMichel Weststratebefore adoption895e0cc

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–2why, one step back
1---
2id: complex-objects

Better sidebar titleMichel Weststratebefore adoption39c6e72

line 3why, one step back
3title: Working with Map, Set and classes

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 4–7why, one step back
4---
56<div id="codefund"><!-- fallback content --></div>7

Updated docsMichel Weststratebefore adoption895e0cc

lines 8–9why, one step back
8_⚠ Since version 6 support for `Map`s and `Set`s has to be enabled explicitly by calling [`enableMapSet()`](installation#pick-your-immer-version) once when starting your application._
9

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 10–25why, one step back
10Plain objects, arrays, `Map`s and `Set`s are always drafted by Immer. An example of using Maps with immer:
1112```javascript13test("Producers can update Maps", () => {14 const usersById_v1 = new Map()1516 const usersById_v2 = produce(usersById_v1, draft => {17 // Modifying a map results in a new map18 draft.set("michel", {name: "Michel Weststrate", country: "NL"})19 })2021 const usersById_v3 = produce(usersById_v2, draft => {22 // Making a change deep inside a map, results in a new map as well!23 draft.get("michel").country = "UK"24 })25

docs: more of thatMichel Weststratebefore adoption41809f8

line 26why, one step back
26 // We got a new map each time!

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 27–28why, one step back
27 expect(usersById_v2).not.toBe(usersById_v1)
28 expect(usersById_v3).not.toBe(usersById_v2)

docs: more of thatMichel Weststratebefore adoption41809f8

line 29why, one step back
29 // With different content obviously

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 30–46why, one step back
30 expect(usersById_v1).toMatchInlineSnapshot(`Map {}`)
31 expect(usersById_v2).toMatchInlineSnapshot(`32 Map {33 "michel" => Object {34 "country": "NL",35 "name": "Michel Weststrate",36 },37 }38 `)39 expect(usersById_v3).toMatchInlineSnapshot(`40 Map {41 "michel" => Object {42 "country": "UK",43 "name": "Michel Weststrate",44 },45 }46 `)

docs: more of thatMichel Weststratebefore adoption41809f8

lines 47–52why, one step back
47 // The old one was never modified
48 expect(usersById_v1.size).toBe(0)49 // And trying to change a Map outside a producers is going to: NO!50 expect(() => usersById_v3.clear()).toThrowErrorMatchingInlineSnapshot(51 `"This object has been frozen and should not be mutated"`52 )

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 53–54why, one step back
53})
54```

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 55–75why, one step back
55
56Every other object must use the `immerable` symbol to mark itself as compatible with Immer. When one of these objects is mutated within a producer, its prototype is preserved between copies.5758```js59import {immerable} from "immer"6061class Foo {62 [immerable] = true // Option 16364 constructor() {65 this[immerable] = true // Option 266 }67}6869Foo[immerable] = true // Option 370```7172For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.7374When working with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.75

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

line 76why, one step back
76Maps and Sets that are produced by Immer will be made artificially immutable. This means that they will throw an exception when trying mutative methods like `set`, `clear` etc. outside a producer.

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 77–78why, one step back
77
78_Note: The **keys** of a map are never drafted! This is done to avoid confusing semantics and keep keys always referentially equal_