redux-thunks
Simple thunk creator for redux.
This is meant to smooth over the use of redux-thunk mixed with the use of redux-actions.
Installation
$ yarn add redux-thunks
or
$ npm install --save redux-thunks
Usage
Best used along side redux-actions
.
Action definition
import { createAction } from 'redux-actions';
import { createThunk } from 'redux-thunks';
export const setAmount = createAction('SET_AMOUNT');
export const incrementAsync = createThunk('INCREMENT_ASYNC', ({ dispatch, getState }, amount) => {
doAsyncMath(getState(), amount).then(val => dispatch(setAmount(val)));
});
Use in component/container
dispatch(incrementAsync, 10);
API
createThunk
createThunk('name', { dispatch, [getStore] }, [...args])
The syntax is very similar to that of createAction
, and it smooths over the differences by similarly exposing a toString
method on the thunk creator.
createThunk
takes two arguments, a name, and a function to execute when the action is dispatched. The first argument of the function will be an object with dispatch
and getState
, as provided by redux-thunk
. Any additional arguments will be anything specified at the dispatch call site. It's expected that at least one other action will be dispatched from the handler function.
License
MIT © w33ble