Redux Async Action Middleware
Install
npm install --save redux-async-action-middleware
CommonJS
var asyncMiddleware = default;
ES Modules
;
Why Do I Need This?
Just like Redux Thunk if you're not sure whether you need it, you probably don't.
Motivation
This middleware was inspired by Redux Thunk and Redux Async. After using a variation of Redux Async, I had realized that defining the three LOAD, SUCCESS, and FAIL redux event types were redundant and not necessary for my use. I also didn't like the non-standard organization of metadata vs payload. So following the lead of Redux Promise, I adopted the Flux Standard Action convention. The result is a combination of everything I liked from all three of these middleware modules.
Usage
Redux Thunk
There is a slight variation compared to Redux Thunk Composition
- function (dispatch, getState, extraArg) { ... }+ function ({dispatch, getState, ...extraArgs}) { ... }
Note how instead of three arguments it is now a single object that contains dispatch, getState, and a spread of the extraArg object passed into the middleware creator. This was - in my opinion - a cleaner way to enable easy access to only the needed arguments by leveraging the destructuring feature.
Async Actions
;;;; const http = axios; // Note: this API requires redux@>=3.1.0const store = ; const getThing = type: 'FETCH_THINGS' http; const getStuffedThing = type: 'FETCH_STUFF_THING' http ; store;store;
Async Actions Redux Store Access
;;; @ static propTypes = foo: PropTypesobjectisRequired getFoo: PropTypesfuncisRequired { const getFoo = thisprops; ; } { const foo: loading loaded payload = thisprops; return <div> loading && <p>loading...</p> loaded && <p>payload</p> </div> }