pitfalls.md
This file
Path
docs/pitfalls.md
72 lines · 3.5 KiB · Markdown
Last changed by
docs: Organize performance and pitfalls, and document nested produce behavior. Fixes #694Michel Weststratebefore adoption0730231
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 back9docs: Organize performance and pitfalls, and document nested produce behavior. Fixes #694Michel Weststratebefore adoption0730231
line 10why, one step back10### Performance tipesfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 11why, one step back11docs: Organize performance and pitfalls, and document nested produce behavior. Fixes #694Michel Weststratebefore adoption0730231
lines 12–41why, one step back12For performance tips, see [Performance Tips](https://immerjs.github.io/immer/docs/performance/#performance-tips).1314### Don't reassign the recipe argument1516Never reassign the `draft` argument (example: `draft = myCoolNewState`). Instead, either modify the `draft` or return a new state. See [Returning data from producers](https://immerjs.github.io/immer/docs/return).1718### Immer only supports unidirectional trees1920Immer assumes your state to be a unidirectional tree. That is, no object should appear twice in the tree, there should be no circular references. There should be exactly one path from the root to any node of the tree.2122### Never explicitly return `undefined` from a producer2324It 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.2526### Don't mutate exotic objects2728Immer [does not support exotic objects](https://github.com/immerjs/immer/issues/504) such as window.location.2930### Classes should be made draftable or not mutated3132You 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).3334### Only valid indices and length can be mutated on Arrays3536For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.3738### Data not originating from the state will never be drafted3940Note 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.41feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 42–55why, one step back42```javascript43function onReceiveTodo(todo) {44 const nextTodos = produce(todos, draft => {45 draft.todos[todo.id] = todo46 // Note, because 'todo' is coming from external, and not from the 'draft',47 // it isn't draft so the following modification affects the original todo!48 draft.todos[todo.id].done = true4950 // The reason for this, is that it means that the behavior of the 2 lines above51 // is equivalent to code, making this whole process more consistent52 todo.done = true53 draft.todos[todo.id] = todo54 })55}docs: Organize performance and pitfalls, and document nested produce behavior. Fixes #694Michel Weststratebefore adoption0730231
lines 56–71why, one step back56```5758### Immer patches are not necessarily optimal5960The 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.6162### Always use the result of nested producers6364Nested `produce` calls are supported, but note that `produce` will _always_ produce a new state, so even when passing a draft to a nested produce, the changes made by the inner produce won't be visibile in the draft that was passed it, but only in the output that is produced. In other words, when using nested produce, you get a draft of a draft and the result of the inner produce should be merged back into the original draft (or returned). For example `produce(state, draft => { produce(draft.user, userDraft => { userDraft.name += "!" })})` won't work as the output if the inner produce isn't used. The correct way to use nested producers is:6566```javascript67produce(state, draft => {68 draft.user = produce(draft.user, userDraft => {69 userDraft.name += "!"70 })71})feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 72why, one step back72```