redux: suggestion, don't allow store to dispatch action.type of undefined or null

I’ve haven’t given this a lot of deep though so I’ll probably someday want this post erased from the internet, but a few times already I’ve screwed up my ‘constants’,

You can see in the below example, if I misspell ACCRUE either here or in the constants file (I’m not even sure if its spelled correctly here), it becomes undefined and then the reducer happily uses the default switch, and because store.dispatch is inclined to put whatever it pleases (@@init/etc) through the default path, I can’t use that to catch screw ups. Why not throw an exception when a falsey action.type is dispatched, since its so easy to do and sometimes difficult to catch, even in tests.

     switch ( action.type ) {                                                                                                                        
       case constants.AUTH_TOKEN:                                                                                                                    
         return authenticate(state, action)                                                                                                          
       case constants.ACCRUE:                                                                                                                        
         return accrue(state, action)                                                                                                                
       default:                                                                                                                                      
         return state                                                                                                                                
     }   

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 17 (13 by maintainers)

Most upvoted comments

But what if there is some logic inside the action creator that decides that there is no need to proceed with dispatching (just think about any conditional logic that uses getState)? Now you can “abort” dispatching, just by returning {} from the action creator (not really abort, just reducers will ignore this action entirely). But if the action type will be required, workaround will be way more verbose: {type: PLEASE_IGNORE_ME } + having PLEASE_IGNORE_ME in the constants file