Skip to content
Myr

complex-objects.md

This file

Path

docs/complex-objects.md

74 lines · 2.2 KiB · Markdown

Last changed by

docs: more of thatMichel Weststratebefore adoption41809f8

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–7why, one step back
1---
2id: complex-objects3title: Working with classes4---56<div id="codefund"><!-- fallback content --></div>7

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 8–23why, one step back
8Plain objects, arrays, `Map`s and `Set`s are always drafted by Immer. An example of using Maps with immer:
910```javascript11test("Producers can update Maps", () => {12 const usersById_v1 = new Map()1314 const usersById_v2 = produce(usersById_v1, draft => {15 // Modifying a map results in a new map16 draft.set("michel", {name: "Michel Weststrate", country: "NL"})17 })1819 const usersById_v3 = produce(usersById_v2, draft => {20 // Making a change deep inside a map, results in a new map as well!21 draft.get("michel").country = "UK"22 })23

docs: more of thatMichel Weststratebefore adoption41809f8

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

docs: added Map exampleMichel Weststratebefore adoption85361a7

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

docs: more of thatMichel Weststratebefore adoption41809f8

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

docs: added Map exampleMichel Weststratebefore adoption85361a7

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

docs: more of thatMichel Weststratebefore adoption41809f8

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

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 51–52why, one step back
51})
52```

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 53–73why, one step back
53
54Every 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.5556```js57import {immerable} from "immer"5859class Foo {60 [immerable] = true // Option 16162 constructor() {63 this[immerable] = true // Option 264 }65}6667Foo[immerable] = true // Option 368```6970For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.7172When working with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.73

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

line 74why, one step back
74Maps 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.