Skip to content
Myr

complex-objects.md

This file

Path

docs/complex-objects.md

30 lines · 1007 bytes · Markdown

Last changed by

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

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

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

line 8why, one step back
8Plain objects, arrays, `Map`s and `Set`s are always drafted by Immer.

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 9–29why, one step back
9
10Every 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```2526For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.2728When working with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.29

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

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