-
-
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
Feature: expose the reducers
mapping object supplied to combineReducers()
#2613
Comments
For the sake of illustrating the actual modification I'm suggesting, it would look something like this: // change this:
return function combination(state = {}, action) { ... }
// to this:
const combination = function(state = {}, action) { ... }
combination._reducers = reducers
return combination https://github.com/reactjs/redux/blob/master/src/combineReducers.js#L132 |
Busy atm, but at first glance I think I can get behind this. We've got prior precedent with |
A few topics for discussion if anyone has opinions:
I'm currently leaning towards 1)
... from my example would turn into:
|
It would seem you can do this with a higher-order/enhancer function: function combineAndSaveReducers(reducers) {
const combinedReducers = combineReducers(reducers)
combinedReducers.reducers = reducers
return combinedReducers
} |
@timdorr This is what I'm currently doing. I'm just advocating that the resulting utility benefits make this a compelling core feature. Without a standard target included in core, multiple enhancers trying to introspect against the |
It doesn't seem like a necessary feature to me. You can either use a HoF like that, or you can simply collect the input to let currentReducers
export function buildCombineReducers(reducers) {
currentReducers = reducers
return combineReducers(reducers)
}
export const getCurrentReducers = () => currentReducers I don't think we need to add this to the core library. |
@timdorr I agree that the workaround is trivial for a single enhancer. The problem is when multiple enhancers (especially from independent module developers) need to introspect, they have nothing to coordinate against.
Interoperability between independent enhancers requires a common target, and it's unlikely to see that emerge outside of core (as evidenced by the present lack of a common target, despite there already being substantial community activity in this area). |
@timdorr Any chance you could weigh in on the merits (or lack thereof) for this being an interoperable solution? I think that's the biggest limitation of simple HoF-based solutions. A secondary advantage would be that libraries like this could be enhancer-only, rather than enhancer + custom reducer. |
While it is trivial, I think it would be nice to have an official way to access the current reducers. It'd help when writing |
The reducer returned from
combineReducers()
is currently an opaque function with respect to the original reducers that comprise it. While this is typically desirable, there may be benefits to allowing enhancers to perform some introspection.Example Use Case:
Several store enhancers currently exist [1] that allow "slices" of state to be dynamically added and removed from the store. Related behavior has also been provided by Dan Abramov[2].
If the initial reducers object was attached to
combination
[3] (we'll assign it to the_reducers
property for this example), the functionality described above could trivially be achieved using a store enhancer:Why not just create another module that does this?
I wholly respect that
combineReducers()
is neither intended nor presented as the one-true approach to slice management, but I also support paving cowpaths. This is a two-line addition with no backwards compatibility concerns or API changes relevant to regular usage. In exchange, it opens the door to some useful reducer manipulation, including patterns like the one in the example that assist with both code organization and code splitting. The benefit of including this in redux core vs. a new module is the increased interoperability of these enhancers, as they have a consistent and ubiquitously used property to target (i.e.,_reducers
).Thoughts?
[1] redux-inject-reducer, paradux, redux-dynamix, redux-react-dynamic-store, ReducerRegistry
[2] https://stackoverflow.com/questions/32968016/how-to-dynamically-load-reducers-for-code-splitting-in-a-redux-application/33044701#33044701
[3] https://github.com/reactjs/redux/blob/master/src/combineReducers.js#L132
The text was updated successfully, but these errors were encountered: