pitfalls.md
This file
Path
docs/pitfalls.md
12 lines · 1.4 KiB · Markdown
Last changed by
Merge pull request #423 from medifle/patch-1Michel Weststratebefore adoption60c4a4f
History of this fileTake it
pitfalls.md
id: pitfalls title: Pitfalls
- Don't redefine draft like,
draft = myCoolNewState. Instead, either modify thedraftor return a new state. See Returning data from producers. - 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.
- 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
currentStaterather than thedraftState. 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 thatoriginalcan be used to get the original state of an object, which is cheaper to read. - Always try to pull
produce'up', for examplefor (let x of y) produce(base, d => d.push(x))is exponentially slower thanproduce(base, d => { for (let x of y) d.push(x)}) - It is possible to return values from producers, except, it is not possible to return
undefinedthat way, as it is indistinguishable from not updating the draft at all! If you want to replace the draft withundefined, just returnnothingfrom the producer.