Thunks
Use listeners instead
While thunks definitely work, it's recommended to use the newer listeners plugin instead. Listeners support everything thunks do and more. Plus they are integrated directly into kea.
Thunks are simple ways to define side effects in Kea. They use redux-thunk
under the hood.
Installation#
First install the kea-thunk and redux-thunk packages:
Then install the plugin:
Usage#
You define thunks in a block called thunks. Whatever you define there can be called through logic.actions,
for example in the useActions hook or directly inside the logic:
Here is an example of thunks in action:
As you can see, you have access to the standard Redux dispatch and getState methods.
However you don't need to call dispatch before any action in the actions object.
They are wrapped automatically.
Note about autoConnect#
The current thunk plugin (v1.0.0) does not support autoConnect. That means if you want to call otherLogic.actions.something()
inside a thunk, you must first make sure otherLogic is connected to your logic: