pitfalls.md
This file
Path
docs/pitfalls.md
35 lines · 2.8 KiB · Markdown
Last changed by
docs: clarify that patches are not optimal. Solves #648Michel Weststratebefore adoption41673f4
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–5why, one step back1---2id: pitfalls3title: Pitfalls4---5docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 6–8why, one step back6<center>7<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>8</center>Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
line 9why, one step back9Immer 7Michel Weststratebefore adoption467ea5d
line 10why, one step back101. For performance tips, see [Performance Tips](https://immerjs.github.io/immer/docs/performance/#performance-tips).Merge pull request #423 from medifle/patch-1Michel Weststratebefore adoption60c4a4f
line 11why, one step back111. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](https://immerjs.github.io/immer/docs/return).Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 12–14why, one step back121. Immer assumes your state to be a unidirectional tree. That is, no object should appear twice in the tree, and there should be no circular references.131. Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`. Also, realize that immer is opt-in everywhere, so it is perfectly fine to manually write super performance critical reducers, and use immer for all the normal ones. Also note that `original` can be used to get the original state of an object, which is cheaper to read.141. It is possible to return values from producers, except, it is not possible to return `undefined` that way, as it is indistinguishable from not updating the draft at all! If you want to replace the draft with `undefined`, just return `nothing` from the producer.Merge pull request #529 from reubn/patch-1Michel Weststratebefore adoption4218668
line 15why, one step back151. Immer [does not support exotic objects](https://github.com/immerjs/immer/issues/504) such as window.location.Merge pull request #544 from delanni/patch-1Michel Weststratebefore adoptionb94c34f
line 16why, one step back161. You will need to enable your own classes to work properly with Immer. For docs on the topic, check out the section on [working with complex objects](https://immerjs.github.io/immer/docs/complex-objects).Immer 7Michel Weststratebefore adoption467ea5d
line 17why, one step back171. For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 18why, one step back181. Note that data that comes from the closure, and not from the base state, will never be drafted, even when the data has become part of the new draft:docs: clarify that patches are not optimal. Solves #648Michel Weststratebefore adoption41673f4
line 19why, one step back191. The set of patches generated by Immer should be correct, that is, applying them to an equal base object should result in the same end state. However Immer does not guarantee the generated set of patches will be optimal, that is, the minimum set of patches possible.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 20–35why, one step back2021```javascript22function onReceiveTodo(todo) {23 const nextTodos = produce(todos, draft => {24 draft.todos[todo.id] = todo25 // Note, because 'todo' is coming from external, and not from the 'draft',26 // it isn't draft so the following modification affects the original todo!27 draft.todos[todo.id].done = true2829 // The reason for this, is that it means that the behavior of the 2 lines above30 // is equivalent to code, making this whole process more consistent31 todo.done = true32 draft.todos[todo.id] = todo33 })34}35```