DEV Community

Ibrahim Shamma
Ibrahim Shamma

Posted on • Updated on

asyncdispatch is now on npm packages!

async-dispatch middleware

npm

async-dispatch is a middleware can be added with no time to privilage the asynchronous into your store

For more informatiom about the future of redux-logger, check out the discussion here.

Table of contents

Install

npm i --save async-dispatch

Usage

import asyncDispatchMiddleware from "async-dispatch";
import { createStore } from "redux";
import { applyMiddleware } from "redux";

const store = createStore(
  rootReducer,
  applyMiddleware(asyncDispatchMiddleware)
);
// Note passing middleware as the third argument requires redux@>=3.1.0
Enter fullscreen mode Exit fullscreen mode

Then applying this middleware is simple as

const LoginReducer = (state = intialState, action) => {
      login(action.userData).then((data) => {
        if (data.error) {
          action.asyncDispatch({
            type: actionTypes.ERROR,
            message: data.error,
          });
        } else {
          action.asyncDispatch({
            type: actionTypes.SUCCESS,
            user: data,
            message: "Logged successfully",
          });
        }
      });
      return { ...state };
})

Enter fullscreen mode Exit fullscreen mode

To Do

  • [ ] Adding types

License

MIT

Top comments (0)