Skip to content
Myr

complex-objects.md

This file

Path

docs/complex-objects.md

73 lines · 2.2 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–2why, one step back
1---
2id: complex-objects

Immer 7Michel Weststratebefore adoption467ea5d

line 3why, one step back
3title: Classes

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 4–5why, one step back
4---
5

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

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

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 9why, one step back
9

Immer 7Michel Weststratebefore adoption467ea5d

line 10why, one step back
10Plain objects (objects without a prototype), arrays, `Map`s and `Set`s are always drafted by Immer. Every 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.

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

line 11why, one step back
11

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 12–25why, one step back
12```js
13import {immerable} from "immer"1415class Foo {16 [immerable] = true // Option 11718 constructor() {19 this[immerable] = true // Option 220 }21}2223Foo[immerable] = true // Option 324```25

Immer 7Michel Weststratebefore adoption467ea5d

line 26why, one step back
26### Example

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 27why, one step back
27

Immer 7Michel Weststratebefore adoption467ea5d

lines 28–29why, one step back
28```js
29import {immerable, produce} from "immer"

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 30why, one step back
30

Immer 7Michel Weststratebefore adoption467ea5d

lines 31–32why, one step back
31class Clock {
32 [immerable] = true

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

line 33why, one step back
33

Immer 7Michel Weststratebefore adoption467ea5d

lines 34–73why, one step back
34 constructor(hour, minute) {
35 this.hour = hour36 this.minute = minute37 }3839 get time() {40 return `${this.hour}:${minute}`41 }4243 tick() {44 return produce(this, draft => {45 draft.minute++46 })47 }48}4950const clock1 = new Clock(12, 10)51const clock2 = clock1.tick()52console.log(clock1.time) // 12:1053console.log(clock2.time) // 12:1154console.log(clock2 instanceof Clock) // true55```5657### Semantics in detail5859The semantics on how classes are drafted are as follows:60611. A draft of a class is a fresh object but with the same prototype as the original object.622. When creating a draft, Immer will copy all _own_ properties from the base to the draft.This includes non-enumerable and symbolic properties.633. _Own_ getters will be invoked during the copy process, just like `Object.assign` would.644. Inherited getters and methods will remain as is and be inherited by the draft.655. Immer will not invoke constructor functions.666. The final instance will be constructed with the same mechanism as the draft was created.677. Only getters that have a setter as well will be writable in the draft, as otherwise the value can't be copied back.6869Because Immer will dereference own getters of objects into normal properties, it is possible to use objects that use getter/setter traps on their fields, like MobX and Vue do.7071Immer does not support exotic objects such as DOM Nodes or Buffers.7273So when working for example with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.