Skip to content
Myr

docs: es6 map support

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Alec Larson · committed by Alec Larson · parent 8276a6c · Browse files · 1 file+3 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+3 added−16 removed

  • other:+3 added−16 removed

1 file+3 added−16 removed

modified:readme.md+3 added-16 removed
526 unmodified lines
527```528529## Supported object types530531Plain objects and arrays are always drafted by Immer.531Plain objects, arrays, and `Map` objects are always drafted by Immer.532533Every 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.534535```js
13 unmodified lines
549For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.550551When working with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.552553Built-in classes like `Map` and `Set` are not supported. As a workaround, you should clone them before mutating them in a producer:553`Set` objects cannot be drafted. As a workaround, you should clone them before mutating them in a producer:554555```js556const state = {557 set: new Set(),558 map: new Map()557 set: new Set()558}559const nextState = produce(state, draft => {560 // Don't use any Set methods, as that mutates the instance!561 draft.set.add("foo") // ❌
5 unmodified lines
567 newSet.add("foo")568569 // 3. Update the draft with the new set570 draft.set = newSet572573 // Similarly, don't use any Map methods.574 draft.map.set("foo", "bar") // ❌575576 // 1. Instead, clone the map (just once)577 const newMap = new Map(draft.map) // ✅578579 // 2. Mutate it580 newMap.set("foo", "bar")581582 // 3. Update the draft583 draft.map = newMap571})572```573574## TypeScript or Flow
203 unmodified lines