@dmartss/thunk
v1.0.3
Published
This is my attempt to replicate the redux thunk package to learn
Downloads
13
Maintainers
Readme
Redux Thunk
Thunk middleware for Redux.
This is my attempt to replicate the redux thunk package as a practice for creating my own NPM packages.
npm install --save @dmartss/thunk
yarn add @dmartss/thunk
Motivation
Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods dispatch
and getState
as parameters.
Installation
To enable @dmartss/thunk, use applyMiddleware()
:
import { createStore, applyMiddleware } from 'redux'
import thunk from '@dmartss/thunk'
import rootReducer from './reducers/index'
// Note: this API requires redux@>=3.1.0
const store = createStore(rootReducer, applyMiddleware(thunk))
Composition
Any return value from the inner function will be available as the return value of dispatch
itself. This is convenient for orchestrating an asynchronous control flow with thunk action creators dispatching each other and returning Promises to wait for each other’s completion: