1.0.8 • Published 8 years ago
redux-amr v1.0.8
Redux async middleware and reducer
This package wil help you dispatch async action with less boilerplate.
Install
npm install redux-amr --save
How to use
store/configureStore.js
import { asyncMiddleware } from 'redux-amr';
applyMiddleware(thunk, asyncMiddleware)
reducers/index.js
import { combineReducers } from 'redux';
import { reducerCreator } from 'redux-amr';
const rootReducer = combineReducers({
async: reducerCreator()
});
export default rootReducer;
actions/index.js
import { ASYNC } from 'redux-amr';
/**
* This actionCreator will create LOAD and LOAD_SUCCESS,
* state.async[key] will be 'success'
*/
function success() {
return {
[ASYNC]: {
key: 'key',
promise: () => Promise.resolve('success')
}
}
}
/**
* This actionCreator will create LOAD and LOAD_FAIL,
* state.async.loadState.[key].error will be 'fail'
*/
function fail() {
return {
[ASYNC]: {
key: 'key',
promise: () => Promise.reject('fail')
}
}
}
Action and state
action
- LOAD: data loading for particular key is started
- LOAD_SUCCESS: data loading process successfully finished. You'll have data returned from promise
- LOAD_FAIL: data loading process was failed. You'll have error returned from promise
state
API
[ASYNC]
key
: Stringpromise
: Function => Promisestore
(Option): Object
reducerCreator
: Function => Reducerreducers
(Option): Object
License
MIT