Skip to content
Myr

docs: Update readme

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

authored by Michel Weststrate · committed by Alec Larson · parent 71f7bbe · Browse files · 1 file+29 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

1 file+29 added−2 removed

  • other:+29 added−2 removed

1 file+29 added−2 removed

modified:readme.md+29 added-2 removed
600 unmodified lines
601const state: State = {602 x: 0603}604605const newState = produce<State>(state, draft => {605const newState = produce(state, draft => {606 // `x` can be modified here607 draft.x++608})609
1 unmodified line
611```612613This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!614615**Note:** Immer v1.9+ supports Typescript v3.1+ only.615For curried reducers, the type is inferred from the first argument of recipe function, so make sure to type it. The `Draft` utility type can be used if the state argument type is immutable:616617```ts618import produce, {Draft} from "immer"619620interface State {621 readonly x: number622}623624// `x` cannot be modified here625const state: State = {626 x: 0627}628629const increment = produce((draft: Draft<State>, inc: number) => {630 // `x` can be modified here631 draft.x += inc632})633634const newState = increment(state, 2)635// `newState.x` cannot be modified here636```637638**Note:** Immer v1.9+ supports Typescript v3.1+ only. **Note:** Immer v2.2+ supports Typescript v3.4+ only.639640## Using `this`641642The recipe will be always invoked with the `draft` as `this` context.643
118 unmodified lines
762**Immer 1.\* -> 2.0**763764Make sure you don't return any promises as state, because `produce` will actually invoke the promise and wait until it settles.765766**Immer 2.1 -> 2.2**767768When using TypeScript, for curried reducers that are typed in the form `produce<Type>((arg) => { })`, rewrite this to `produce((arg: Type) => { })` or `produce((arg: Draft<Type>) => { })` for correct inference.769770## FAQ771772_(for those who skimmed the above instead of actually reading)_773
35 unmodified lines