1.2.0 • Published 6 years ago
redux-api-handling-middleware v1.2.0
Redux Api Middleware
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-middlewareDocs 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).