redux-act-async
Create async actions and reducers based on redux-act
Install
npm install redux-act-async --save
Badges
Usage
;; // The async api to call, must be a function that returns a promiselet user = id: 8;{ return Promise;} // createActionAsync will create 4 synchronous action creators:// login.request, login.ok, login.error and login.resetconst login = ; /*createReducerAsync takes an async action created by createActionAsync.It reduces the following state given the four actions: request, ok, error and reset.const defaultsState = { loading: false, request: null, data: null, error: null}; if you need to overwrite the defaultsState just insert your initialState as a second paramenter in the createReducerAsync function. Just like that: const initialState = { loading: false, request: null, data: {custom: "intitial data"}, error: null}; const reducer = createReducerAsync(login, initialState) */const reducer = const store = ; await store;
Legacy redux
In a nutshell, the following code:
const options = noRethrow: false;const loginAction = ;const loginReducer =
is equivalent to:
const LOGIN_REQUEST = 'LOGIN_REQUEST'const LOGIN_OK = 'LOGIN_OK'const LOGIN_ERROR = 'LOGIN_ERROR'const LOGIN_RESET = 'LOGIN_RESET' const loginRequest = type: LOGIN_REQUEST payload: value const loginOk = type: LOGIN_OK payload: value const loginError = type: LOGIN_ERROR payload: value const loginReset = type: LOGIN_RESET payload: value const options = noRethrow: true; const login = { return { ; return }} const defaultsState = loading: false request: null data: null error: null; const reducer = ;
That's 3 lines against 78 lines, a good way to reduce boilerplate code.
Async Action Options
Here are all the options to configure an asynchronous action:
const actionOptions = noRethrow: false request: { } { return payload } { return ASYNC_METAREQUEST } ok: { } { return payload } { return ASYNC_METAOK } error: { } { return payload } { return ASYNC_METAERROR } const loginAction = ;
Who is using this library ?
This library has been extracted originally from starhack.it, a React/Node Full Stack Starter Kit.