0.0.5 • Published 5 years ago

react-redux-api-middleware v0.0.5

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

Redux API Middleware

Loading - Success - Error, Redux pattern. Just a little bit less boilerplate.

npm install react-redux-api-middleware

How does it work

  1. Create a Redux action and specify network request details.
const FETCH_USERS = createActionSet('FETCH_USERS');

const action = () => ({
  [API_CALL]: {
    url: '/some/endpoint',
    method: 'GET',
    actions: FETCH_USERS,
  },
});
  1. Once you dispatch an action, middleware will perform a network request and dispatch Loading, Success or Error actions.
function userReducer(state, action) {
  switch(action.type) {
    case: FETCH_USERS.LOADING:
      // Do something with the loading state
      return state;
    case FETCH_USERS.SUCESS: {
      // Do something with the response
      return {
        ...state,
        users: action.response,
      }
    }
    case FETCH_USERS.ERROR: {
      return {
        ...state,
        someErrorMessage: action.error,
      }
    }
    ...
  }
}
  1. Profit
0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago