@dmartss/thunk
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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:

Package Sidebar

Install

npm i @dmartss/thunk

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

9.91 kB

Total Files

9

Last publish

Collaborators

  • dmartss