Skip to content
Myr

complex-objects.md

This file

Path

website/docs/complex-objects.md

73 lines · 2.3 KiB · Markdown

Last changed by

chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3

History of this file

Take it

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 1–6why, one step back
1---
2id: complex-objects3title: Classes4---56<center>

chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3

line 7why, one step back
7<div data-ea-publisher="immerjs" data-ea-type="image" className="horizontal bordered"></div>

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 8–70why, one step back
8</center>
910Plain 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.1112```js13import {immerable} from "immer"1415class Foo {16 [immerable] = true // Option 11718 constructor() {19 this[immerable] = true // Option 220 }21}2223Foo[immerable] = true // Option 324```2526### Example2728```js29import {immerable, produce} from "immer"3031class Clock {32 [immerable] = true3334 constructor(hour, minute) {35 this.hour = hour36 this.minute = minute37 }3839 get time() {40 return `${this.hour}:${this.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.70

docs: clarify that Map / Set cant be subclassed or made immerable. Fixes #781Michel Weststratebefore adoption0af1143

line 71why, one step back
71Immer does not support exotic / engine native objects such as DOM Nodes or Buffers, nor is subclassing Map, Set or arrays supported and the `immerable` symbol can't be used on them.

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 72–73why, one step back
72
73So when working for example with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.