react-redux-api-middleware
v0.0.5
Published
Loading - Success - Error, Redux pattern. Just a little bit less boilerplate. ``` npm install react-redux-api-middleware ```
Downloads
3
Readme
Redux API Middleware
Loading - Success - Error, Redux pattern. Just a little bit less boilerplate.
npm install react-redux-api-middleware
How does it work
- 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,
},
});
- 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,
}
}
...
}
}
- Profit