Skip to content
Myr

Updated docs. Also fixes #445

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

authored by Michel Weststrate · committed by Michel Weststrate · parent e819e79 · Browse files · 7 files+16 added−37 removed
This commit

State

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

Diff stats

7 files+16 added−37 removed

  • tests:+6 added−0 removed
  • other:+10 added−37 removed

7 files+16 added−37 removed

modified:__tests__/frozen.js+6 added
modified:docs/api.md+1 added
18 unmodified lines
19| `nothing` | Value that can be returned from a recipe, to indicate that the value `undefined` should be produced | [Return](return.md) |20| `original` | Given a draft object (doesn't have to be a tree root), returns the the original object at the same path in the original state tree, if present | [Original](original.md) |21| `Patch` | Exposed TypeScript type, describes the shape of an (inverse) patch object | [Patches](patches.md) |22| `produce` | The core API of Immer, also exposed as the `default` export | [Produce](produce.md) |23| `produceWithPatches` | Works the same as `produce`, but instead of just returning the produced object, it returns a tuple, consisting of `[result, patches, inversePatches]`. | [Patches](patches.md) |24| `setAutoFreeze` | Enables / disables automatic freezing of the trees produces. By default enabled in development builds | [Freezing](freezing.md) |25| `setUseProxies` | Can be used to disable or force the use of `Proxy` objects. Useful when filing bug reports. | |2627## Importing immer
14 unmodified lines
modified:docs/complex-objects.md+2 added-34 removed
3 unmodified lines
4---56<div id="codefund"><!-- fallback content --></div>78Plain objects and arrays are always drafted by Immer.8Plain objects, arrays, `Map`s and `Set`s are always drafted by Immer.910Every 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```js
13 unmodified lines
26For 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.2930Built-in classes like `Map` and `Set` are not yet supported (they might be if there is popular demand). As a workaround, you should clone them before mutating them in a producer:3132```js33const state = {34 set: new Set(),35 map: new Map()36}37const nextState = produce(state, draft => {38 // Don't use any Set methods, as that mutates the instance!39 draft.set.add("foo") // ❌4041 // 1. Instead, clone the set (just once)42 const newSet = new Set(draft.set) // ✅4344 // 2. Mutate the clone (just in this producer)45 newSet.add("foo")4647 // 3. Update the draft with the new set48 draft.set = newSet4950 // Similarly, don't use any Map methods.51 draft.map.set("foo", "bar") // ❌5253 // 1. Instead, clone the map (just once)54 const newMap = new Map(draft.map) // ✅5556 // 2. Mutate it57 newMap.set("foo", "bar")5859 // 3. Update the draft60 draft.map = newMap61})62```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.
modified:docs/faq.md+5 added-1 removed
18 unmodified lines
19A: Yes2021## Q: I can't rely on Proxies being present on my target environments. Can I use Immer?2223A: Yes23A: Yes [details](http://localhost:3000/immer/docs/installation#immer-on-older-javascript-environments)2425## Q: Can I typecheck my data structures when using Immer?2627A: Yes
1 unmodified line
29## Q: Can I store `Date` objects, functions etc in my state tree when using Immer?3031A: Yes3233## Q: Can I use Maps and Sets?3435A: Yes3637## Q: Is it fast?3839A: Yes40
3 unmodified lines
modified:docs/introduction.md+1 added-1 removed
52 unmodified lines
53Head to the [next section](produce) to further dive into `produce`.5455## Benefits5657- Immutability with normal JavaScript objects and arrays. No new APIs to learn!57- Immutability with normal JavaScript objects, arrays, Sets and Maps. No new APIs to learn!58- Strongly typed, no string based paths selectors etc.59- Structural sharing out of the box60- Object freezing out of the box61- Deep updates are a breeze
3 unmodified lines
modified:docs/performance.md+1 added-1 removed
25 unmodified lines
26Here is a [simple benchmark](https://github.com/immerjs/immer/blob/master/__performance_tests__/todo.js) on the performance of Immer. This test takes 50,000 todo items and updates 5,000 of them. _Freeze_ indicates that the state tree has been frozen after producing it. This is a _development_ best practice, as it prevents developers from accidentally modifying the state tree.2728Something that isn't reflected in the numbers above, but in reality, Immer is sometimes significantly _faster_ than a hand written reducer. The reason for that is that Immer will detect "no-op" state changes, and return the original state if nothing actually changed, which can avoid a lot of re-renderings for example. Cases are known where simply applying immer solved critical performance issues.2930These tests were executed on Node 10.15.1. Use `yarn test:perf` to reproduce them locally.30These tests were executed on Node 10.16.3. Use `yarn test:perf` to reproduce them locally.3132![performance.png](/immer/img/performance.png)3334Most important observation:
5 unmodified lines
modified:website/static/img/performance.pngbinary

A binary file, modified.