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
Provenance
- Commit
5479196- Tree
4c5a5bc- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
b0d3ce3
1 file changed+71 added−69 removed
modified:readme.md+71 added-69 removed
333 unmodified lines334 For 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)335 336 Tip: Check this trick to [compress patches](https://medium.com/@david.b.edelstein/using-immer-to-compress-immer-patches-f382835b6c69) produced over time.337 338-## Auto freezing339-340-Immer 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.341-338 ## Returning data from producers339 340 It 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 lines417 ```418 419 N.B. Note that this problem is specific for the `undefined` value, any other value, including `null`, doesn't suffer from this issue.420 421+## Inline shortcuts using `void`422+423+Draft 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.424+425+In 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`.426+427+```javascript428+// Single mutation429+produce(draft => void (draft.user.age += 1))430+431+// Multiple mutations432+produce(draft => void ((draft.user.age += 1), (draft.user.height = 186)))433+```434+435+Code 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.436+437 ## Extracting the original object from a proxied instance438 439 Immer 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 lines458 })459 isDraft(nextState) // => false460 ```461 450-## Using `this`462+## Auto freezing463 452-The recipe will be always invoked with the `draft` as `this` context.464+Immer 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.465 454-This means that the following constructions are also valid:455-456-```javascript457-const base = {counter: 0}458-459-const next = produce(base, function() {460- this.counter++461-})462-console.log(next.counter) // 1463-464-// OR465-const increment = produce(function() {466- this.counter++467-})468-console.log(increment(base).counter) // 1469-```470-471-## Inline shortcuts using `void`472-473-Draft 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.474-475-In 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`.476-477-```javascript478-// Single mutation479-produce(draft => void (draft.user.age += 1))480-481-// Multiple mutations482-produce(draft => void ((draft.user.age += 1), (draft.user.height = 186)))483-```484-485-Code 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.486-487-## TypeScript or Flow488-489-The 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.490-491-The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:492-493-```ts494-import produce from "immer"495-496-interface State {497- readonly x: number498-}499-500-// `x` cannot be modified here501-const state: State = {502- x: 0503-}504-505-const newState = produce<State>(state, draft => {506- // `x` can be modified here507- draft.x++508-})509-510-// `newState.x` cannot be modified here511-```512-513-This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!514-515-**Note:** Immer v1.9+ supports Typescript v3.1+ only.516-466 ## Immer on older JavaScript environments?467 468 By 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 lines524 draft.map = newMap525 })526 ```527 528+## TypeScript or Flow529+530+The 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.531+532+The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:533+534+```ts535+import produce from "immer"536+537+interface State {538+ readonly x: number539+}540+541+// `x` cannot be modified here542+const state: State = {543+ x: 0544+}545+546+const newState = produce<State>(state, draft => {547+ // `x` can be modified here548+ draft.x++549+})550+551+// `newState.x` cannot be modified here552+```553+554+This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!555+556+**Note:** Immer v1.9+ supports Typescript v3.1+ only.557+558+## Using `this`559+560+_Deprecated, this will probably be removed in a next major version, see [#308](https://github.com/mweststrate/immer/issues/308)_561+562+The recipe will be always invoked with the `draft` as `this` context.563+564+This means that the following constructions are also valid:565+566+```javascript567+const base = {counter: 0}568+569+const next = produce(base, function() {570+ this.counter++571+})572+console.log(next.counter) // 1573+574+// OR575+const increment = produce(function() {576+ this.counter++577+})578+console.log(increment(base).counter) // 1579+```580+581 # Pitfalls582 583 1. 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).584 1. 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