Write More Organizable Actions and Reducers with Less Code
This package redux-declare
allows you to create Actions and Reducers with objects like
{type: function}
and {type: {status: function}}
.
Feature
- Promise-like Async Actions: Dispatch Async Actions with Status Change.
- Declarative Reducers and Actions: As easy as Redux-act/Redux-actions for Synchronic operations.
- Neater thunk code: Do not need to rewrite
type
andstatus
in thunk code.
Install
Just like other packages by npm:
npm i --save redux-declare
Document
A Sync Counter Example
Following is an redux store reprsenting a counter with sync actions (sub and add).
;;; let nestedReducers = count: statecount + actioncount count: statecount - actioncount ; let reducer = ;let actionCreators = ; let add sub = actionCreators; store;; store;;
An Async Counter Example
Following we create a redux store representing a counter with async actions. In
this counter, add
action is always synchronic. sub
action is synchronic
when action.status !== success
, and is asynchronous when action.status === success
.
;;;; let nestedReducers = count: statecount + actioncount sub: // Activated when sub action is pending and state.paused is true count: statecount - actioncount // Activated when sub action is pending and state.paused is true count: statecount + 001 // state.paused controls whether sub async action // would render a success result or an error result paused: actionpaused || !statepaused ; let nestedActions = sub: { ; } ; // Options default optionslet reducer = ;let store = ;
Then we could examine the synchronic and asynchronous actions and reducers by the code below
let actionCreators = ;let add sub pause = actionCreators; // Test Sync Actionsstore;; store;; // Test Async Actionsstore;; ;
FAQ:
-
Do I need to rewrite
type
orstatus
when customizing action creators?No. As long as you are trying to dispatching the
type
andstatus
by the node indexed by the sametype
andstatus
, you need not to retype thetype
andstatus
. Even you are dispatch action in thunk, it is not necessary to rewrite thetype
andstatus
.
Details could be seen insrc/composeAutoFix.js
.
Maybe In the Plan:
- Webpack bundle compile
- Compose Dispatch for avoid retype action.type
- Support FSA by appending rules
-
BindAll
like in the `redux-act`