Skip to content

Commit

Permalink
Add default prop to README (#32)
Browse files Browse the repository at this point in the history
Add default prop to README
  • Loading branch information
esamattis authored Jul 10, 2019
2 parents 1c757d5 + e5ebca9 commit af27477
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@ function* handleImmerReducerAction(action: Actions<typeof MyImmerReducer>) {
}
```

**Warning:** Due to how immer-reducers action generation works, adding default
parameters to the methods will NOT pass it to the action payload, which can
make your reducer impure and the values will not be available in middlewares.

```ts
class MyImmerReducer extends ImmerReducer<State> {
addItem (id: string = uuid()) {
this.draftState.ids.push([id])
}
}

immerActions.addItem() // generates empty payload { payload: [] }
```

As a workaround, create custom action creator wrappers that pass the default parameters instead.

```ts
class MyImmerReducer extends ImmerReducer<State> {
addItem (id) {
this.draftState.ids.push([id])
}
}

const actions = {
addItem: () => immerActions.addItem(id)
}
```


## 📚 Examples

Here's a more complete example with redux-saga and [redux-render-prop](https://github.com/epeli/redux-render-prop):
Expand Down

0 comments on commit af27477

Please sign in to comment.