-
-
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
Preserve any extra keys for composability #2059
Conversation
Travis build error is not related to PR. All tests are passing. |
I agree with @markerikson that Some people want extra keys removed (see #1748, for example), whereas some people want extra keys retained (such as yourself). I think you could make convincing arguments either way. The bottom line is that A variation of However, I doubt there is a compelling reason that this should be the default behavior for everyone, so generally I think I'd be opposed to this PR. It's easy to build what you need in user land or release a 3rd party library with your own version of |
For what its worth I just came upon this PR because I had this exact same issue today, using this (simplified) reducer pattern...
This pattern is useful when there is also an optional While I understand that Its such a simple change it seems a shame to have to recreate the entire thing from scratch, perhaps an optional |
I understand warnings could be useful, but I don't understand how removing keys helps anyone, except confusing. On top, you make it impossible to follow your own guidelines:
With You're also loosely breaking one of the rules of redux:
Strictly speaking, you're not breaking this rule, but if reducer passed to Removing extra keys is not a behavior isn't documented anywhere (yet). But instead of documenting it, I suggest removing this side effect all together. |
It's completely feasible to build an adapter in front of combineReducers that does what you want without recreating anything from scratch. Here is an example: const adapter = (reducerHash) => {
const combined = combineReducers(reducerHash);
return (state, action) => {
return Object.assign({}, state, combined(state, action));
}
} Then, wherever you would normally call If you want to avoid the warnings too you can enhance this example to restrict what you pass to My point is simply that you don't have to start from scratch. |
@naw is right on this. |
This is a fix for behavior noticed in #2058 that is considered by @markerikson as "working as intended", but effectively making reducers produced by combineReducers not composable. I strongly disagree with this opinion, so I ask for second opinion of @gaearon before you close this PR.
PR solves an issue where I compose reducer produced by
combineReducers
with other reducer (for example by usingreduceReducers
, but also simple applying):If otherReducer returns keys not used in
combinedReducer
, thencombinedReducer
not only shows a warning thatUnexpected key "extra" found in previous state received by the reducer
(what I can leave with evenrually), but also removes extra keys set byotherReducer
.This severly limits composability of reducers, and I think it shoudn't be a default behavior of redux.
You can see simplest example possible in test I attach to this PR, where an extra key is present in state passed to reducer produced with combineReducers, but this keys is missing in result.
This modification is fully backward compatible, I hope you agree to include it.