-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
About combineReducers #1062
Comments
Thanks for the write-up! I'm closing as a duplicate of #1024, #883. Please see the discussion there. From http://redux.js.org/docs/recipes/ReducingBoilerplate.html#generating-reducers:
|
Hi, I want to share a conceptual problem I have with combineReducers in order to have the community opinion.
Example app
I have 3 actions : INCREMENT (step), DECREMENT (step), FOO;
With combineReducers
I will have 2 reducers, I only show the count one below.
P1 : the switch on action.type
This is my first problem. It sounds like an "instanceof" in OOP world very rarely used because it generally reveals that the model has an architectural problem solvable with polymorphism concept.
I think the problem is because we want here to mix functional programmation (the reducer) with Object oriented (the action payload represents an object with a type).
A solution : divide & conquer. If we split the reducer with a constraint 1 handler = 1 action
Note : the partial solution is just here to understand the thought.
P2 : the "default" boilerplate
we must define the default case with a simple identity return. But why ? the lib may handle this factorization for us.
This problem is a direct consequence of the combineReducers broadcasting. Indeed combineReducers act as a broadcaster but I think a router here may be a better choice. With a router I will not have the
default
or theif (map[action.type] !== undefined)
anymore.The performance also is concerned but .. I know it's clearly not the breaking point of a real app so I don't hold this argument.
P3 : the signature
It's very difficult to maintain a big app without an explicit signature. Here we have an "action" but we don't know what is composed of and it is impossible because it can be all the actions (a growing set during the lifecycle of a development of an app).
"atomic" reducers
To solve all these conceptual problems I want to introduce atomic reducer : a reducer handling only one action type.
Ok but ..
Better :).
I also need a map to call the good reducer on an action. It will also have the responsibility to decrypt the action payload
And finally my main store
See complete example https://gist.github.com/jnoleau/8c30f8f4f1e70ea18c7d
Conclusion
I understand combineReducers is just an example of a rootReducer but in fact as it's included in the library I think a lot of people use it as a best practice. Maybe a solution could be to extract the combineReducers in another npm package ?
I would really appreciate your opinion about "atomic" router approach.
Thx.
The text was updated successfully, but these errors were encountered: