complex-objects.md
This file
Path
docs/complex-objects.md
76 lines · 2.4 KiB · Markdown
Last changed by
Document and test things that are _not_ draftedMichel Weststratebefore adoption6e76122
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–2why, one step back1---2id: complex-objectsBetter sidebar titleMichel Weststratebefore adoption39c6e72
line 3why, one step back3title: Working with Map, Set and classesMerge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 4–7why, one step back4---56<div id="codefund"><!-- fallback content --></div>7docs: added Map exampleMichel Weststratebefore adoption85361a7
lines 8–23why, one step back8Plain 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 })23docs: more of thatMichel Weststratebefore adoption41809f8
line 24why, one step back24 // We got a new map each time!docs: added Map exampleMichel Weststratebefore adoption85361a7
lines 25–26why, one step back25 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 back27 // With different content obviouslydocs: added Map exampleMichel Weststratebefore adoption85361a7
lines 28–44why, one step back28 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 back45 // The old one was never modified46 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 back51})52```Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 53–73why, one step back5354Every 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.73feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 74why, one step back74Maps 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.Document and test things that are _not_ draftedMichel Weststratebefore adoption6e76122
lines 75–76why, one step back7576_Note: The **keys** of a map are never drafted! This is done to avoid confusing semantics and keep keys always referentially equal_