Redux Heretic
Reduce the amount of boilerplate code needed to create a Redux app, by merging actions and reducers into the same definition 🙀.
Examples
Counter
Plain redux counter example:
// Reducer { if typeof state === 'undefined' return 0; } // Action factory { return type: 'INCREMENT';} { return type: 'DECREMENT';} const store = Redux;store;
Using redux-heretic:
Each key of the object passed to redux-heretic contains a reducer function. The returned object has action factories and a reducer that will apply the state change based on the action type.
; const actions reducer = ; const store = Redux;store;
Custom action factory
Each value in the object passed to redux-heretic accepts a function or an object.
The function variant specifies a reducer. The object variant allows to pass an action factory and/or a reducer.
const actions reducer = ; const store = Redux; // add(10) calls the create function with: create('ADD', 10) to// get value of the actionstore; // you can also access to the action typeconsole; // --> ADD
Prefix action types
When combining reducers, you need to make sure that action types are unique. You can pass a prefix, and all the action types will use it.
const actions reducer = ; console; // --> COUNTER_INCREMENT
Async action with redux-thunk
You can define an action factory without a reducer. Also, each action factory
receives an actions
instance, so you can create other actions to dispatch.
This is helpful when using libraries like redux-thunk.
;;; const actions reducer = ; const store = Redux;store;
Custom action type names
By default, action names have the format: PREFIX_ACTION_NAME
.
For example startIncrement
with the prefix counter
, becomes
COUNTER_START_INCREMENT
.
To change the format pass a typeFormat
function to the options:
; const actions reducer = ; console;// shows: startIncrement/a
Non 1-1 relationship between action-reducer
Redux Heretic makes easy to create a 1-1 relationship between action and
reducer. But, actions are not necessarily related to one reducer.
Is possible to define only action factories using create
:
const actions = ; // we only care of action factories;
In that case, the library also helps to reduce the amount of boilerplate code
using the actionFactories
helper:
; { return { ; ; }; } 'counter';
Heads up! Note the
/helpers
module path.
But what about reducers? Do I need to code the usual switch
statement to
handle an action defined elsewhere?
You can use Redux Heretic for that too, but it's a little bit verbose:
const reducer = ;;
For those cases the library provides the handy reducer
function:
; myActionFactorytype: { // ... return ...state /* ... */; };
Reducing boilerplate code of action factories
The previous section describes how to write less code when creating action
factories without a reducer. If your action factories are dumb, you can reduce
the amount of code even more with the helpers defaultActionFactory
and
defaultActionFactories
.
Before:
; type ...payload;
With defaultActionFactory
:
; dumbAction: defaultActionFactory;
Using defaultActionFactories
:
; ;
License
MIT