Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Apr 15, 2019
1 parent f7769d7 commit 3fdfcc6
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions readme.md
Expand Up @@ -531,14 +531,14 @@ Plain objects and arrays are always drafted by Immer.
Every other object must use the `immerable` symbol to mark itself as compatible with Immer. When one of these objects is mutated within a producer, its prototype is preserved between copies.

```js
import {immerable} from 'immer'
import {immerable} from "immer"

class Foo {
[immerable] = true // Option 1
[immerable] = true // Option 1

constructor() {
this[immerable] = true // Option 2
}
constructor() {
this[immerable] = true // Option 2
}
}

Foo[immerable] = true // Option 3
Expand Down Expand Up @@ -600,7 +600,7 @@ const state: State = {
x: 0
}

const newState = produce<State>(state, draft => {
const newState = produce(state, draft => {
// `x` can be modified here
draft.x++
})
Expand All @@ -610,7 +610,30 @@ const newState = produce<State>(state, draft => {

This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!

**Note:** Immer v1.9+ supports Typescript v3.1+ only.
For 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:

```ts
import produce, {Draft} from "immer"

interface State {
readonly x: number
}

// `x` cannot be modified here
const state: State = {
x: 0
}

const increment = produce((draft: Draft<State>, inc: number) => {
// `x` can be modified here
draft.x += inc
})

const newState = increment(state, 2)
// `newState.x` cannot be modified here
```

**Note:** Immer v1.9+ supports Typescript v3.1+ only. **Note:** Immer v2.2+ supports Typescript v3.4+ only.

## Using `this`

Expand Down Expand Up @@ -736,6 +759,10 @@ Most important observation:

Make sure you don't return any promises as state, because `produce` will actually invoke the promise and wait until it settles.

**Immer 2.1 -> 2.2**

When 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.

## FAQ

_(for those who skimmed the above instead of actually reading)_
Expand Down

0 comments on commit 3fdfcc6

Please sign in to comment.