Skip to content
Myr

docs: reordered readme a bit to a more logical order

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 b0d3ce3 · Browse files · 1 file+71 added−69 removed
This commit

State

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

Diff stats

1 file+71 added−69 removed

  • other:+71 added−69 removed

1 file+71 added−69 removed

modified:readme.md+71 added-69 removed
333 unmodified lines
334For a more in-depth study, see [Distributing patches and rebasing actions using Immer](https://medium.com/@mweststrate/distributing-state-changes-using-snapshots-patches-and-actions-part-2-2f50d8363988)335336Tip: Check this trick to [compress patches](https://medium.com/@david.b.edelstein/using-immer-to-compress-immer-patches-f382835b6c69) produced over time.337338## Auto freezing339340Immer automatically freezes any state trees that are modified using `produce`. This protects against accidental modifications of the state tree outside of a producer. This comes with a performance impact, so it is recommended to disable this option in production. It is by default enabled. By default, it is turned on during local development and turned off in production. Use `setAutoFreeze(true / false)` to explicitly turn this feature on or off.341338## Returning data from producers339340It is not needed to return anything from a producer, as Immer will return the (finalized) version of the `draft` anyway. However, it is allowed to just `return draft`.341
75 unmodified lines
417```418419N.B. Note that this problem is specific for the `undefined` value, any other value, including `null`, doesn't suffer from this issue.420421## Inline shortcuts using `void`422423Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with.424425In such cases, you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.426427```javascript428// Single mutation429produce(draft => void (draft.user.age += 1))430431// Multiple mutations432produce(draft => void ((draft.user.age += 1), (draft.user.height = 186)))433```434435Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft => { draft.user.age += 1}` to avoid cognitive overhead.436437## Extracting the original object from a proxied instance438439Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce` (or return `undefined` for unproxied values). A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.440
17 unmodified lines
458})459isDraft(nextState) // => false460```461450## Using `this`462## Auto freezing463452The recipe will be always invoked with the `draft` as `this` context.464Immer automatically freezes any state trees that are modified using `produce`. This protects against accidental modifications of the state tree outside of a producer. This comes with a performance impact, so it is recommended to disable this option in production. It is by default enabled. By default, it is turned on during local development and turned off in production. Use `setAutoFreeze(true / false)` to explicitly turn this feature on or off.465454This means that the following constructions are also valid:455456```javascript457const base = {counter: 0}458459const next = produce(base, function() {460 this.counter++461})462console.log(next.counter) // 1463464// OR465const increment = produce(function() {466 this.counter++467})468console.log(increment(base).counter) // 1469```470471## Inline shortcuts using `void`472473Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with.474475In such cases, you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.476477```javascript478// Single mutation479produce(draft => void (draft.user.age += 1))480481// Multiple mutations482produce(draft => void ((draft.user.age += 1), (draft.user.height = 186)))483```484485Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft => { draft.user.age += 1}` to avoid cognitive overhead.486487## TypeScript or Flow488489The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.490491The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:492493```ts494import produce from "immer"495496interface State {497 readonly x: number498}499500// `x` cannot be modified here501const state: State = {502 x: 0503}504505const newState = produce<State>(state, draft => {506 // `x` can be modified here507 draft.x++508})509510// `newState.x` cannot be modified here511```512513This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!514515**Note:** Immer v1.9+ supports Typescript v3.1+ only.516466## Immer on older JavaScript environments?467468By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.469
54 unmodified lines
524 draft.map = newMap525})526```527528## TypeScript or Flow529530The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.531532The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:533534```ts535import produce from "immer"536537interface State {538 readonly x: number539}540541// `x` cannot be modified here542const state: State = {543 x: 0544}545546const newState = produce<State>(state, draft => {547 // `x` can be modified here548 draft.x++549})550551// `newState.x` cannot be modified here552```553554This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!555556**Note:** Immer v1.9+ supports Typescript v3.1+ only.557558## Using `this`559560_Deprecated, this will probably be removed in a next major version, see [#308](https://github.com/mweststrate/immer/issues/308)_561562The recipe will be always invoked with the `draft` as `this` context.563564This means that the following constructions are also valid:565566```javascript567const base = {counter: 0}568569const next = produce(base, function() {570 this.counter++571})572console.log(next.counter) // 1573574// OR575const increment = produce(function() {576 this.counter++577})578console.log(increment(base).counter) // 1579```580581# Pitfalls5825831. 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).5841. 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.
128 unmodified lines