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 dd82d89 · Browse files · 1 file+1 added−35 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+1 added−35 removed

  • other:+1 added−35 removed

1 file+1 added−35 removed

modified:readme.md+1 added-35 removed
526 unmodified lines
527```528529## Supported object types530531Plain objects and arrays are always drafted by Immer.531Plain objects, arrays, `Map` objects, and `Set` 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:554555```js556const state = {557 set: new Set(),558 map: new Map()559}560const nextState = produce(state, draft => {561 // Don't use any Set methods, as that mutates the instance!562 draft.set.add("foo") // ❌563564 // 1. Instead, clone the set (just once)565 const newSet = new Set(draft.set) // ✅566567 // 2. Mutate the clone (just in this producer)568 newSet.add("foo")569570 // 3. Update the draft with the new set571 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 = newMap584})585```586553## TypeScript or Flow554555The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.556
200 unmodified lines