Simple redux utils
Installation
npm install --save simple-redux-utils
Usage
Import the module in your application:
// From ES6; // From CJSconst reduxUtils = ; // From globalconst reduxUtils = simpleReduxUtilsdefault;
Documentation
Reducers
# createReducer(initialState, handler)
const reducer = ;
Return the initialState if no action match.
initialState
Type: any
handler
Type: { actionType: (previousState, action) => nextState }
| default: {}
Actions
# asyncActionWith(fn, ...actions)(...params)(dispatch)
const fn = Promise; const request = type: 'REQUEST' payload; const success = type: 'SUCCESS' payload; const failure = type: 'FAILURE' payload; const action = id; ;
fn
Type: (...params: Array<any>): Promise<any>
The function to execute in the action creator.
actions
Type: Array<(payload: any) => ACTION>
Array of 3 action creators (REQUEST, SUCCESS, FAILURE).
params
Type: any
Parameters for the function (will be dispatch in REQUEST action).
dispatch
Type: (ACTION) => any
Redux dipatch function.
# bindActionToPromise(actionCreator)(dispatch)
const actionCreator = type: 'ACTION' payload const boundAction = ; ; // Usage with Redux Saga { const action = ; const resolver = ; try ; ; resolver; catcherror resolver; }
actionCreator
Type: (payload: any) => ACTION
payload
Type: any
dispatch
Type: (ACTION) => any
Redux dipatch function.
# getPromiseResolverBoundToPromise(action)
const resolver = ; resolver;resolver;
action
Type: ACTION
Sagas
# throws(error)
const saga = { ; try const response = ; ; response; catch error // We can handle error at this level ; ; } const main = { try ; ; ; catch error // Or at this one // Useful for dispatch different action ; }
error
Type: { [KEY: any]: any }
# wrapSagaWithResolver(saga, action)
const watch = { while true const action = ; ; }; const boundAction = ;
saga
Type: function* () => any
action
Type: ACTION
MUST have been bound to promise with bindActionToPromise
.
Run the test
npm test