Beyond combineReducers

The combineReducers utility included with Redux is very useful, but is deliberately limited to handle a single common use case: updating a state tree that is a plain Javascript object, by delegating the work of updating each slice of state to a specific slice reducer. It does not handle other use cases, such as a state tree made up of Immutable.js Maps, trying to pass other portions of the state tree as an additional argument to a slice reducer, or performing "ordering" of slice reducer calls. It also does not care how a given slice reducer does its work.

The common question, then, is "How can I use combineReducers to handle these other use cases?". The answer to that is simply: "you don't - you probably need to use something else". Once you go past the core use case for combineReducers, it's time to use more "custom" reducer logic, whether it be specific logic for a one-off use case, or a reusable function that could be widely shared. Here's some suggestions for dealing with a couple of these typical use cases, but feel free to come up with your own approaches.

Using slice reducers with Immutable.js objects

Since combineReducers currently only works with plain Javascript objects, an application that uses an Immutable.js Map object for the top of its state tree could not use combineReducers to manage that Map. Since many developers do use Immutable.js, there are a number of published utilities that provide equivalent functionality, such as redux-immutable. This package provides its own implementation of combineReducers that knows how to iterate over an Immutable Map instead of a plain Javascript object.

Sharing data between slice reducers

Similarly, if sliceReducerA happens to need some data from sliceReducerB's slice of state in order to handle a particular action, or sliceReducerB happens to need the entire state as an argument, combineReducers does not handle that itself. This could be resolved by writing a custom function that knows to pass the needed data as an additional argument in those specific cases, such as:

  1. function combinedReducer(state, action) {
  2. switch (action.type) {
  3. case 'A_TYPICAL_ACTION': {
  4. return {
  5. a: sliceReducerA(state.a, action),
  6. b: sliceReducerB(state.b, action)
  7. }
  8. }
  9. case 'SOME_SPECIAL_ACTION': {
  10. return {
  11. // specifically pass state.b as an additional argument
  12. a: sliceReducerA(state.a, action, state.b),
  13. b: sliceReducerB(state.b, action)
  14. }
  15. }
  16. case 'ANOTHER_SPECIAL_ACTION': {
  17. return {
  18. a: sliceReducerA(state.a, action),
  19. // specifically pass the entire state as an additional argument
  20. b: sliceReducerB(state.b, action, state)
  21. }
  22. }
  23. default:
  24. return state
  25. }
  26. }

Another alternative to the "shared-slice updates" issue would be to simply put more data into the action. This is easily accomplished using thunk functions or a similar approach, per this example:

  1. function someSpecialActionCreator() {
  2. return (dispatch, getState) => {
  3. const state = getState()
  4. const dataFromB = selectImportantDataFromB(state)
  5. dispatch({
  6. type: 'SOME_SPECIAL_ACTION',
  7. payload: {
  8. dataFromB
  9. }
  10. })
  11. }
  12. }

Because the data from B's slice is already in the action, the parent reducer doesn't have to do anything special to make that data available to sliceReducerA.

A third approach would be to use the reducer generated by combineReducers to handle the "simple" cases where each slice reducer can update itself independently, but also use another reducer to handle the "special" cases where data needs to be shared across slices. Then, a wrapping function could call both of those reducers in turn to generate the final result:

  1. const combinedReducer = combineReducers({
  2. a: sliceReducerA,
  3. b: sliceReducerB
  4. })
  5. function crossSliceReducer(state, action) {
  6. switch (action.type) {
  7. case 'SOME_SPECIAL_ACTION': {
  8. return {
  9. // specifically pass state.b as an additional argument
  10. a: handleSpecialCaseForA(state.a, action, state.b),
  11. b: sliceReducerB(state.b, action)
  12. }
  13. }
  14. default:
  15. return state
  16. }
  17. }
  18. function rootReducer(state, action) {
  19. const intermediateState = combinedReducer(state, action)
  20. const finalState = crossSliceReducer(intermediateState, action)
  21. return finalState
  22. }

As it turns out, there's a useful utility called reduce-reducers that can make that process easier. It simply takes multiple reducers and runs reduce() on them, passing the intermediate state values to the next reducer in line:

  1. // Same as the "manual" rootReducer above
  2. const rootReducer = reduceReducers(combinedReducers, crossSliceReducer)

Note that if you use reduceReducers, you should make sure that the first reducer in the list is able to define the initial state, since the later reducers will generally assume that the entire state already exists and not try to provide defaults.

Further Suggestions

Again, it's important to understand that Redux reducers are just functions. While combineReducers is useful, it's just one tool in the toolbox. Functions can contain conditional logic other than switch statements, functions can be composed to wrap each other, and functions can call other functions. Maybe you need one of your slice reducers to be able to reset its state, and to only respond to specific actions overall. You could do:

  1. const undoableFilteredSliceA = compose(
  2. undoReducer,
  3. filterReducer('ACTION_1', 'ACTION_2'),
  4. sliceReducerA
  5. )
  6. const rootReducer = combineReducers({
  7. a: undoableFilteredSliceA,
  8. b: normalSliceReducerB
  9. })

Note that combineReducers doesn't know or care that there's anything special about the reducer function that's responsible for managing a. We didn't need to modify combineReducers to specifically know how to undo things - we just built up the pieces we needed into a new composed function.

Also, while combineReducers is the one reducer utility function that's built into Redux, there's a wide variety of third-party reducer utilities that have published for reuse. The Redux Addons Catalog lists many of the third-party utilities that are available. Or, if none of the published utilities solve your use case, you can always write a function yourself that does just exactly what you need.