1.2.0 • Published 5 years ago

redux-api-handling-middleware v1.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

Redux Api Middleware

npm version npm downloads

Redux Api middleware enables robust handling of async code in Redux. The middleware enables optimistic updates and dispatches success and failed actions. It can be used to chain async actions. It allows you to write action creators that return a function instead of an action, you can dispatch more than action in the action creator function.

Installation

npm i redux-api-handling-middleware

Docs and Help

Usage

import apiMiddleware from  'redux-api-handling-middleware';
import axios from 'axios';

const middleware = applyMiddleware(apiMiddleware.withClient(axios));
const store = createStore(reducer, middleware);
//sample ACTION
export function getSeatLayout() {
  return (dispatch) => {
    dispatch({
      type: 'FETCH_LAYOUT',
      promise: client => client.get('/layout'),
    });
    dispatch({
      type: 'UPDATE_SEAT_REQ',
      noOfSeats: 2,
    });
  };
}

After dispatching action of type FETCH_LAYOUT our middleware automatically dispatches FETCH_LAYOUT_SUCCESS and FETCH_LAYOUT_FAILED according to the api response.


Copyright (c) 2017 Pranav TV. Licensed with The MIT License (MIT).