complex-objects.md
This file
Path
docs/complex-objects.md
78 lines · 2.5 KiB · Markdown
Last changed by
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
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>7Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 8–9why, one step back8_⚠ 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._9docs: added Map exampleMichel Weststratebefore adoption85361a7
lines 10–25why, one step back10Plain 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 })25docs: more of thatMichel Weststratebefore adoption41809f8
line 26why, one step back26 // We got a new map each time!docs: added Map exampleMichel Weststratebefore adoption85361a7
lines 27–28why, one step back27 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 back29 // With different content obviouslydocs: added Map exampleMichel Weststratebefore adoption85361a7
lines 30–46why, one step back30 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 back47 // The old one was never modified48 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 back53})54```Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 55–75why, one step back5556Every 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.75feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 76why, one step back76Maps 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 back7778_Note: The **keys** of a map are never drafted! This is done to avoid confusing semantics and keep keys always referentially equal_