Skip to content
Myr

Emphasized in readme that state should be a tree

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 f1ca2f6 · Browse files · 2 files+11 added−2 removed
This commit

State

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

Diff stats

2 files+11 added−2 removed

  • tests:+8 added−0 removed
  • other:+3 added−2 removed

2 files+11 added−2 removed

modified:__tests__/base.js+8 added
1217 unmodified lines
1218 it("immer should have no dependencies", () => {1219 expect(require("../package.json").dependencies).toEqual(undefined)1220 })12211222 it("#174", () => {1223 const nextState = produce([1, 2, 3], s => {1224 s.pop()1225 s.push(100)1226 })1227 expect(nextState).toEqual([1, 2, 100])1228 })12291230 afterEach(() => {1231 expect(baseState).toBe(origBaseState)1232 expect(baseState).toEqual(createBaseState())1233 })
21 unmodified lines
modified:readme.md+3 added-2 removed
418 unmodified lines
419import unleashTheMagic from "immer"420import { produce as unleashTheMagic } from "immer"421```422423## Pitfalls423## Limitations / pitfalls4244251. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](#returning-data-from-producers).4261. Currently, Immer only supports plain objects and arrays.4261. Immer only supports plain objects and arrays.4271. Immer assumes your state to be a unidirectional tree. That is, no object should appear twice and there should be no circular references.4281. Class instances are not, and will not be supported. Read here more on why that would be a [confusing, conceptual mismatch]4291. Immer only processes native arrays and plain objects (with a prototype of `null` or `Object`). Any other type of value will be treated verbatim! So if you modify a `Map` or `Buffer` (or whatever complex object from the draft state), the changes will be persisted. But, both in your new and old state! So, in such cases, make sure to always produce fresh instances if you want to keep your state truly immutable.4301. For example, working with `Date` objects is no problem, just make sure you never modify them (by using methods like `setYear` on an existing instance). Instead, always create fresh `Date` instances. Which is probably what you were unconsciously doing already.4311. 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.
165 unmodified lines