tj / frontend-boilerplate

webpack-react-redux-babel-autoprefixer-hmr-postcss-css-modules-rucksack-boilerplate (unmaintained, I don't use it anymore)
2.93k stars 268 forks source link

assemble redux stuffs, merge actions and reducers in a single file. #35

Open Kaijun opened 8 years ago

Kaijun commented 8 years ago

I think it may be a better approach of combining actions and reducers in one file.

Action names will be implicitly declared as variables, so that the action and reducer are able to use the identical variable as their action names.

It's really easy to control the specific domain of actions and reducers. In the long term, when the action names are getting more and more and probably having naming conflict, redux dispatcher will go wrong. But we can simply define the const variables by using Symbol instead of String, for example:

//previously
const ADD_TODO = 'add todo'
const DELETE_TODO = 'delete todo'
const EDIT_TODO = 'edit todo'
const COMPLETE_TODO = 'complete todo'
const COMPLETE_ALL = 'complete all'
const CLEAR_COMPLETE = 'clear complete'

//using Symbol
const ADD_TODO = Symbol('todo')
const DELETE_TODO = Symbol('todo')
const EDIT_TODO = Symbol('todo')
const COMPLETE_TODO = Symbol('todo')
const COMPLETE_ALL = Symbol('todo')
const CLEAR_COMPLETE = Symbol('todo')

Tested it works like a charm :smile:

tj commented 8 years ago

Nice! I'll think about it. In some ways I like separate files, some of my actions get a little large, but in general this seems fine to me, having the constants there for reference is nice. The only thing I'd change is the dir names, not a huge fan of .the ./redux dir, not like we have a ./react dir etc.