From e5ebca9beefffdc8c6cf8b63e64c24beec26c792 Mon Sep 17 00:00:00 2001 From: Arian Allenson Valdez Date: Sat, 6 Jul 2019 17:37:11 +0800 Subject: [PATCH] Add default prop doc --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 033535e..2ef5acb 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,35 @@ function* handleImmerReducerAction(action: Actions) { } ``` +**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 { + 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 { + 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):