- Notifications
You must be signed in to change notification settings - Fork60
An opinionated lib to create actions and reducers for Redux
License
pauldijou/redux-act
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An opinionated lib to create actions and reducers forRedux. The main goal is to use actions themselves as references inside the reducers rather than string constants.
# NPMnpm install redux-act --save# Yarnyarn add redux-act
You can also use abrowser friendly compiled file or theminified version from NPM CDN (mostly for online demo / snippets).
Browser support: this lib usesString.prototype.startsWith
which is not supported by IE11. Be sure to add a polyfill if you are targeting this browser.Learn more.
Even if there is a function namedcreateAction
, it actually creates anaction creator
according to Redux glossary. It was just a bit overkill to name the functioncreateActionCreator
. If you are not sure if something is an action or an action creator, just remember that actions are plain objects while action creators are functions.
// Import functionsimport{createStore}from'redux';import{createAction,createReducer}from'redux-act';// Create an action creator (description is optional)constadd=createAction('add some stuff');constincrement=createAction('increment the state');constdecrement=createAction('decrement the state');// Create a reducer// (ES6 syntax, see Advanced usage below for an alternative for ES5)constcounterReducer=createReducer({[increment]:(state)=>state+1,[decrement]:(state)=>state-1,[add]:(state,payload)=>state+payload,},0);// <-- This is the default state// Create the storeconstcounterStore=createStore(counterReducer);// Dispatch actionscounterStore.dispatch(increment());// counterStore.getState() === 1counterStore.dispatch(increment());// counterStore.getState() === 2counterStore.dispatch(decrement());// counterStore.getState() === 1counterStore.dispatch(add(5));// counterStore.getState() === 6
Does it work with Redux devtools? Yes.
Do reducers work with combineReducers? Of course, they are just normal reducers after all. Remember that according to the
combineReducers
checks, you will need to provide a default state when creating each reducer before combining them.How does it work? There is not much magic. A generated id is prepended to each action type and will be used inside reducers instead of the string constants used inside Redux by default.
Can you show how different it is from writing classic Redux? Sure, you can check both commits to updatecounter example andtodomvc example. You can also run both examples with
npm install && npm start
inside each folder.Why having two syntax to create reducers? The one with only a map of
action => reduce function
doesn't allow much. This is why the other one is here, in case you would need a small state inside the reducer, having something similar as an actor, or whatever you feel like. Also, one of the syntax is ES6 only.Inside a reducer, why is it
(state, payload) => newState
rather than(state, action) => newState
? You can find more info about that on thecreateReducer
API below, but basically, that's because an action is composed of metadata handled by the lib and your payload. Since you only care about that part, better to have it directly. You can switch back to the full action if necessary of course.Why have you done that? Aren't string constants good enough? I know that the Redux doc states that such magic isn't really good, that saving a few lines of code isn't worth hiding such logic. I can understand that. And don't get me wrong, the main goal of this lib isn't to reduce boilerplate (even if I like that it does) but to use the actions themselves as keys for the reducers rather than strings which are error prone. You never know what the new dev on your project might do... Maybe (s)he will not realize that the new constant (s)he just introduced was already existing and now everything is broken and a wormhole will appear and it will be the end of mankind. Let's prevent that!
import{createStore}from'redux';import{createAction,createReducer}from'redux-act';// You can create several action creators at once// (but that's probably not the best way to do it)const[increment,decrement]=['inc','dec'].map(createAction);// When creating action creators, the description is optional// it will only be used for devtools and logging stuff.// It's better to put something but feel free to leave it empty if you want to.constreplace=createAction();// By default, the payload of the action is the first argument// when you call the action. If you need to support several arguments,// you can specify a function on how to merge all arguments into// an unique payload.letappend=createAction('optional description',(...args)=>args.join(''));// There is another pattern to create reducers// and it works fine with ES5! (maybe even ES3 \o/)conststringReducer=createReducer(function(on){on(replace,(state,payload)=>payload);on(append,(state,payload)=>state+=payload);// Warning! If you use the same action twice,// the second one will override the previous one.},'missing a lette');// <-- Default state// Rather than binding the action creators each time you want to use them,// you can do it once and for all as soon as you have the targeted store// assignTo: mutates the action creator itself// bindTo: returns a new action creator assigned to the storeconststringStore=createStore(stringReducer);replace.assignTo(stringStore);append=append.bindTo(stringStore);// Now, when calling actions, they will be automatically dispatchedappend('r');// stringStore.getState() === 'missing a letter'replace('a');// stringStore.getState() === 'a'append('b','c','d');// stringStore.getState() === 'abcd'// If you really need serializable actions, using string constant rather// than runtime generated id, just use a uppercase description (with eventually some underscores)// and it will be use as the id of the actionconstdoSomething=createAction('STRING_CONSTANT');doSomething(1);// { type: 'STRING_CONSTANT', payload: 1}// Little bonus, if you need to support metadata around your action,// like needed data but not really part of the payload, you add a second functionconstmetaAction=createAction('desc',arg=>arg,arg=>'so meta!');// Metadata will be the third argument of the reduce functioncreateReducer({[metaAction]:(state,payload,meta)=>payload});
Parameters
- description (string, optional): used by logging and devtools when displaying the action. If this parameter is uppercase only, with underscores and numbers, it will be used as the action type without any generated id. You can use this feature to have serializable actions you can share between client and server.
- payloadReducer (function, optional): transform multiple arguments as a unique payload.
- metaReducer (function, optional): transform multiple arguments as a unique metadata object.
Usage
Returns a newaction creator. If you specify a description, it will be used by devtools. By default,createAction
will return a function and its first argument will be used as the payload when dispatching the action. If you need to support multiple arguments, you need to specify apayload reducer in order to merge all arguments into one unique payload.
// Super simple actionconstsimpleAction=createAction();// Better to add a descriptionconstbetterAction=createAction('This is better!');// Support multiple arguments by merging themconstmultipleAction=createAction((text,checked)=>({text, checked}))// Again, better to add a descriptionconstbestAction=createAction('Best. Action. Ever.',(text,checked)=>({text, checked}))// Serializable action (the description will be used as the unique identifier)constserializableAction=createAction('SERIALIZABLE_ACTION_42');
An action creator is basically a function that takes arguments and return an action which has the following format:
type
: generated id + your description.payload
: the data passed when calling the action creator. Will be the first argument of the function except if you specified a payload reducer when creating the action.meta
: if you have provided ametaReducer, it will be used to create a metadata object assigned to this key. Otherwise, it'sundefined
.error
: a boolean indicating if the action is an error according toFSA.
constaddTodo=createAction('Add todo');addTodo('content');// return { type: '[1] Add todo', payload: 'content' }consteditTodo=createAction('Edit todo',(id,content)=>({id, content}));editTodo(42,'the answer');// return { type: '[2] Edit todo', payload: {id: 42, content: 'the answer'}}constserializeTodo=createAction('SERIALIZE_TODO');serializeTodo(1);// return { type: 'SERIALIZE_TODO', payload: 1 }
An action creator has the following methods:
getType()
Return the generated type that will be used by all actions from this action creator. Useful forcompatibility purposes.
assignTo(store | dispatch)
Remember that you still need to dispatch those actions. If you already have one or more stores, you can assign the action using theassignTo
function. This will mutate the action creator itself. You can pass one store or one dispatch function or an array of any of both.
letaction=createAction();letaction2=createAction();constreducer=createReducer({[action]:(state)=>state*2,[action2]:(state)=>state/2,});conststore=createStore(reducer,1);conststore2=createStore(reducer,-1);// Automatically dispatch the action to the store when calledaction.assignTo(store);action();// store.getState() === 2action();// store.getState() === 4action();// store.getState() === 8// You can assign the action to several stores using an arrayaction.assignTo([store,store2]);action();// store.getState() === 16// store2.getState() === -2
bindTo(store | dispatch)
If you need immutability, you can usebindTo
, it will return a new action creator which will automatically dispatch its action.
// If you need more immutability, you can bind them, creating a new action creatorconstboundAction=action2.bindTo(store);action2();// Not doing anything since not assigned nor bound// store.getState() === 16// store2.getState() === -2boundAction();// store.getState() === 8
assigned() / bound() / dispatched()
Test the current status of the action creator.
constaction=createAction();action.assigned();// false, not assignedaction.bound();// false, not boundaction.dispatched();// false, test if either assigned or boundconstboundAction=action.bindTo(store);boundAction.assigned();// falseboundAction.bound();// trueboundAction.dispatched();// trueaction.assignTo(store);action.assigned();// trueaction.bound();// falseaction.dispatched();// true
raw(...args)
When an action creator is either assigned or bound, it will no longer only return the action object but also dispatch it. In some cases, you will need the action without dispatching it (when batching actions for example). In order to achieve that, you can use theraw
method which will return the bare action. You could say that it is exactly the same as the action creator would behave it if wasn't assigned nor bound.
constaction=createAction().bindTo(store);action(1);// store has been updatedaction.raw(1);// return the action, store hasn't been updated
asError(...args)*
By default, if your payload is an instance ofError
, the action will be tagged as an error. But if you need to use any other kind of payload as an error payload, you can always use this method. It will apply the same payload reducer by setting theerror
totrue
.
constactionCreator=createAction(value=>{if(value>10){returnnewError('Must be less than 10')}return{value:value}})constgoodAction=actionCreator(5)goodAction.error// falseconstbadAction=actionCreator(20)badAction.error// trueconstforcedBadAction=actionCreator.asError(1)forcedBadAction.error// true
Parameters
- handlers (object or function): if
object
, a map of action to the reduce function. Iffunction
, take two attributes: a function to register actions and another one to unregister them. See below. - defaultState (anything, optional): the initial state of the reducer. Must not be empty if you plan to use this reducer inside a
combineReducers
.
Usage
Returns a newreducer. It's kind of the same syntax as theArray.prototype.reduce
function. You can specify how to reduce as the first argument and the accumulator, or default state, as the second one. The default state is optional since you can retrieve it from the store when creating it but you should consider always having a default state inside a reducer, especially if you want to use it withcombineReducers
which make such default state mandatory.
There are two patterns to create a reducer. One is passing an object as a map ofaction creators
toreduce functions
. Such functions have the following signature:(previousState, payload) => newState
. The other one is using a function factory. Rather than trying to explaining it, just read the following examples.
constincrement=createAction();constadd=createAction();// First patternconstreducerMap=createReducer({[increment]:(state)=>state+1,[add]:(state,payload)=>state+payload},0);// Second patternconstreducerFactory=createReducer(function(on,off){on(increment,(state)=>state+1);on(add,(state,payload)=>state+payload);// 'off' remove support for a specific action// See 'Adding and removing actions' section},0);
Like everything, a reducer is just a function. It takes the current state and an action payload and return the new state. It has the following methods.
options({ payload: boolean, fallback: [handler] })
Since an action is an object with atype
, apayload
(which is your actual data) and eventually somemetadata
, all reduce functions directly take the payload as their 2nd argument and the metadata as the 3rd by default rather than the whole action since all other properties are handled by the lib and you shouldn't care about them anyway. If you really need to use the full action, you can change the behavior of a reducer. Returns the reducer itself for chaining.
constadd=createAction();constsub=createAction();constreducer=createReducer({[add]:(state,action)=>state+action.payload,[sub]:(state,action)=>state-action.payload},0);reducer.options({payload:false});
You can reada more detailed explanation here.
If you specify afallback
handler, which has the exact same signature as any action handler inside the reducer, it will be called anytime you dispatch an action which is not handled by the reducer.
constaction=createAction();constreducer=createReducer({},0);reducer.options({// action is not handled, so fallback will be calledfallback:(state,payload)=>state+payload,});conststore=createStore(reducer);store.getState();// 0store.dispatch(action(5));store.getState();// 5store.dispatch(action(-10));store.getState();// -5
has(action creator)
Test if the reducer has a reduce function for a particular action creator or a string type.
constadd=createAction();constsub=createAction();constreducer=createReducer({[add]:(state,action)=>state+action.payload},0);reducer.has(add);// truereducer.has(sub);// falsereducer.has(add.getType());// true
on(action creator | action creator[], reduce function)off(action creator | action creator[])
You can dynamically add and remove actions. See theadding and removing actions section for more infos. You can use either aredux-act
action creator or a raw string type. You can also use array of those, it will apply theon
oroff
function to all elements.
They both return the reducer itself so you can chain them.
Parameters
- actionCreators (object or array): which action creators to assign. If it's an object, it's a map of name -> action creator, useful when importing several actions at once.
- stores (object or array): the target store(s) when dispatching actions. Can be only one or several inside an array.
Usage
A common pattern is to export a set of action creators as an object. If you want to bind all of them to a store, there is this super small helper. You can also use an array of action creators. And since you can bind to one or several stores, you can specify either one store or an array of stores.
// actions.jsexportconstadd=createAction('Add');exportconstsub=createAction('Sub');// reducer.jsimport*asactionsfrom'./actions';exportdefaultcreateReducer({[actions.add]:(state,payload)=>state+payload,[actions.sub]:(state,payload)=>state-payload},0);// store.jsimport*asactionsfrom'./actions';importreducerfrom'./reducer';conststore=createStore(reducer);assignAll(actions,store);exportdefaultstore;
Parameters
- actionCreators (object or array): which action creators to bind. If it's an object, it's a map of name -> action creator, useful when importing several actions at once.
- stores (object or array): the target store(s) when dispatching actions. Can be only one or several inside an array.
Usage
Just likeassignAll
, you can bind several action creators at once.
import{bindAll}from'redux-act';importstorefrom'./store';import*asactionsfrom'./actions';exportbindAll(actions,store);
Parameters
- actions (objects | array): wrap an array of actions inside another action and will reduce them all at once when dispatching it. You can also call this function with several actions as arguments.
raw
method for such actions. See usage below.
Usage
Useful when you need to run a sequence of actions without impacting your whole application after each one but rather after all of them are done. For example, if you are using@connect
fromreact-redux
, it is called after each action by default. Usingbatch
, it will be called only when all actions in the array have been reduced.
batch
is an action creator like any other created usingcreateAction
. You can assign or bind it if you want, especially if you only have one store. You can even use it inside reducers. It is enabled by default, but you can remove it and put it back.
import{createAction,createReducer,batch}from'redux-act';// Basic actionsconstinc=createAction();constdec=createAction();constreducer=createReducer({[inc]:state=>state+1,[dec]:state=>state-1,},0);conststore=createStore(reducer);// actions as argumentsstore.dispatch(batch(inc(),inc(),dec(),inc()));// actions as an arraystore.dispatch(batch([inc(),inc(),dec(),inc()]));store.getState();// 4// Assigned actionsinc.assignTo(store);dec.assignTo(store);// You still need to dispatch the batch action// You will need to use the 'raw' function on the action creators to prevent// the auto-dipatch from the assigned action creatorsstore.dispatch(batch(inc.raw(),dec.raw(),dec.raw()));store.dispatch(batch([inc.raw(),dec.raw(),dec.raw()]));store.getState();// 2// Let's de-assign our actionsinc.assignTo(undefined);dec.assignTo(undefined);// You can bind batchconstboundBatch=batch.bindTo(store);boundBatch(inc(),inc());store.getState();// 4// You can assign batchbatch.assignTo(store);batch(dec(),dec(),dec());store.getState();// 1// You can remove batch from a reducerreducer.off(batch);batch(dec(),dec());store.getState();// 1// You can put it backreducer.on(batch,(state,payload)=>payload.reduce(reducer,state));batch(dec(),dec());store.getState();// -1
Parameters
- store | dispatch (object, which is a Redux store, or a dispatch function): add a
disbatch
function to the store if it is the only parameter. Just likedispatch
but for several actions which will be batched as a single one. - actions (array, optional): the array of actions to dispatch as a batch of actions.
Usage
// All samples will display both syntax with and without an array// They are exactly the sameimport{disbatch}from'redux-act';import{inc,dec}from'./actions';// Add 'disbatch' directly to the storedisbatch(store);store.disbatch(inc(),dec(),inc());store.disbatch([inc(),dec(),inc()]);// Disbatch immediately from storedisbatch(store,inc(),dec(),inc());disbatch(store,[inc(),dec(),inc()]);// Disbatch immediately from dispatchdisbatch(store.dispatch,inc(),dec(),inc());disbatch(store.dispatch,[inc(),dec(),inc()]);
Parameters
- action (object): a standard Redux action (with a
type
property)
Set theerror
property totrue
.
import{createAction,asError}from'redux-act';constgoodAction=createAction();goodAction.error;// falseconstbadAction=asError(goodAction);badAction.error;// true
This is mostly internal stuff and is exposed only to help during development and testing.
As you know it, each action has a type.redux-act
will ensure that each action creator type is unique. If you are not using serializable actions, you are good to go as all types will be dynamically generated and unique. But if you do use them, by default, nothing prevent you from creating two action creators with the same type.redux-act
will throw if you callcreateAction
with an already used type, and that is good, except when running tests.
During testing, you might need to reset all types, start as fresh, to preventredux-act
to throw between tests. To do so, you have a small API to manage types stored byredux-act
.
import{types}from'redux-act';// Add a type and prevent any action creator from using it from now ontypes.add('MY_TYPE');types.add('MY_TYPE_BIS');// Remove a type, you can use it again in an action creatortypes.remove('MY_TYPE_BIS');// Test if a type is already usedtypes.has('MY_TYPE');// truetypes.has('MY_TYPE_BIS');// false// Check if a type is already used,// will throw TypeError if sotypes.check('MY_TYPE')// throw TypeErrortypes.check('MY_TYPE_BIS')// do nothing (return undefined)// Return all used typestypes.all();// [ 'MY_TYPE' ]// Remove all typestypes.clear();// Disable all type checking meaning you can now have several actions// with the same type. This is needed for HMR (Hot Module Replacement)// but never never never enable it in productiontypes.disableChecking();// Set back type checkingtypes.enableChecking();
redux-act
is fully compatible with any other Redux library, it just usestype
string after all.
redux-act
actions will store data inside thepayload
property and that reducers will automatically extract it by default.
// Mixing basic and redux-act actions inside a reducerimport{createAction,createReducer}from'redux-act';// Standard Redux action using a string constantconstINCREMENT_TYPE='INCREMENT';constincrement=()=>({type:INCREMENT_TYPE});constdecrement=createAction('decrement');constreducer=createReducer({[INCREMENT_TYPE]:(state)=>state+1,[decrement]:(state)=>state-1,},0);reducer.has(INCREMENT_TYPE);// truereducer.has(decrement);// true
// Using redux-act actions inside a basic reducerimport{createAction}from'redux-act';// Standard Redux action using a string constantconstINCREMENT_TYPE='INCREMENT';constincrement=()=>({type:INCREMENT_TYPE});constdecrement=createAction('decrement');functionreducer(state=0,action){switch(action.type){caseINCREMENT_TYPE:returnstate+1;break;casedecrement.getType():returnstate-1;break;default:returnstate;}};
Using the handlers object.
consthandlers={};constreducer=createReducer(handlers,0);conststore=createStore(reducer);constincrement=createAction().assignTo(store);handlers[increment]=(state)=>state+1;increment();// store.getState() === 1increment();// store.getState() === 2delete(handlers[increment]);increment();// store.getState() === 2increment();// store.getState() === 2
Using theon
andoff
functions of the reducer. Those functions will be available whatever pattern you used to create the reducer.
constreducer=createReducer({},0);conststore=createStore(reducer);constincrement=createAction().assignTo(store);reducer.on(increment,(state)=>state+1);increment();// store.getState() === 1increment();// store.getState() === 2reducer.off(increment);increment();// store.getState() === 2increment();// store.getState() === 2
Using theon
andoff
functions of the function factory when creating the reducer.
conststore=createStore(()=>0));constincrement=createAction().assignTo(store);constreducer=createReducer(function(on,off){on(increment,state=>{// Just for fun, we will disable increment when reaching 2// (but we will still increment one last time)if(state===2){off(increment);}returnstate+1;});},0);store.replaceReducer(reducer);increment();// store.getState() === 1increment();// store.getState() === 2increment();// store.getState() === 3increment();// store.getState() === 3increment();// store.getState() === 3
import{createStore,applyMiddleware}from'redux';importthunkMiddlewarefrom'redux-thunk';import{createAction,createReducer}from'redux-act';conststart=createAction();constsuccess=createAction();constreducer=createReducer({[start]:(state)=>({ ...state,running:true}),[success]:(state,result)=>({running:false, result})},{running:false,result:false});// 1) You can use the same way as the Redux samples// using thunk middlewareconstcreateStoreWithMiddleware=applyMiddleware(thunkMiddleware)(createStore);conststore=createStoreWithMiddleware(reducer);functionfetch(){// We don't really need the dispatch// but here it is if you don't bind your actionsreturnfunction(dispatch){// state: { running: false, result: false }dispatch(start());// state: { running: true, result: false }returnnewPromise(resolve=>{// Here, you should probably do a real async call,// like, you know, XMLHttpRequest or Global.fetch stuffsetTimeout(()=>resolve(1),5);}).then(result=>dispatch(success(result))// state: { running: false, result: 1 });};}store.dispatch(fetch()).then(()=>{// state: { running: false, result: 1 }});// 2) You can enjoy the redux-act binding// and directly call the actionsconststore=createStore(reducer);start.assignTo(store);success.assignTo(store);functionfetch(){// state: { running: false, result: false }start();// state: { running: true, result: false }returnnewPromise(resolve=>{// Here, you should probably do a real async call,// like, you know, XMLHttpRequest or Global.fetch stuffsetTimeout(()=>resolve(1),5);}).then(result=>success(result)// state: { running: false, result: 1 });}fetch().then(()=>{// state: { running: false, result: 1 }});
Sincebatch
is an action creator like any other, you can add and remove it from any reducer.
import{createReducer,batch}from'redux-act';constreducer=createReducer({});// Reducer no longer support batched actionsreducer.off(batch);// Put it back using the reducer itself as the reduce functionreducer.on(batch,(state,payload)=>payload.reduce(reducer,state));// Be careful if 'payload' option is falsereducer.options({payload:false});reducer.on(batch,(state,action)=>action.payload.reduce(reducer,state));
We've built some basic typings around this API that will help TypeScript identify potential issues in your code.
You can use any of the existing methods to create reducers and TypeScript will work (as a superset of Javascript) but that kind of defeats some of the benefits of TypeScript. For this reason, the following is the recommended way to create a reducer.
import{createReducer,createAction}from'redux-act';constdefaultState={count:0,otherProperties:any, ...};constadd=createAction<number>('Increase count');constreducer=createReducer<typeofdefaultState>({},defaultState);reducer.on(add,(state,payload)=>({...state,count:state.count+payload}));
Using thereducer.on()
API, TypeScript will identify the payload set onadd
and provide that type as payload. This can be really handy once your code starts scaling up.
Due to some limitations on TypeScript typings, action creators have some limitations but you can create typed action creators assuming you have no payload reducer.
import{createAction}from'redux-act';constaction=createAction<boolean>('Some type');constemptyAction=createAction('Another type');constotherAction=createAction<boolean>('Other action',(arg1,arg2)=>({ arg1, arg2}));
action
andemptyAction
will provide typing support, making sureaction
is provided a boolean as first and only argument, oremptyAction
is not provided any argument at all.
otherAction
, on the otherhand, will be able to be called with any arguments, regardless of what the payload reducer expects.
redux-act
provides improved logging with some loggers, mostly for batched actions.
Missing your favorite one? Please open an issue or a pull request, it will be added as soon as possible.
import{applyMiddleware,createStore}from'redux';importcreateLoggerfrom'redux-logger';import{loggers}from'redux-act';// Init loggerconstlogger=createLogger({ ...loggers.reduxLogger,// You can add you own options after that});// Same asconstlogger=createLogger({actionTransformer:loggers.reduxLogger.actionTransformer,logger:loggers.reduxLogger.logger,// You can add you own options after that});// Create the storeconststore=applyMiddleware(logger)(createStore)(/* reducer */);
A big thank to both@gaearon for creatingRedux and@AlexGalays for creatingfluxx which I took a lot of inspiration from.
If you need to run the tests, just usenpm test
ornpm run coverage
if you want to generate the coverage report.
This software is licensed under the Apache 2 license, quoted below.
Copyright 2015 Paul Dijou (http://pauldijou.fr).
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
About
An opinionated lib to create actions and reducers for Redux