JS Built-ins
redux-legacyrootReducerintermediate

Root Reducer Pattern

πŸ“– Usage Example
function rootReducer(state = {}, action) {
  return {
    counter: counterReducer(state.counter, action),
    name: nameReducer(state.name, action),
  }
}

Manually building a root reducer clarifies how combineReducers works under the hood.

Root Reducer Pattern

A root reducer combines multiple sub-reducers by manually routing state slices:

function rootReducer(state = {}, action) {
  return {
    counter: counterReducer(state.counter, action),
    name: nameReducer(state.name, action),
  }
}

Each sub-reducer only sees and manages its own slice. The root reducer assembles the final state object.

Challenge: Implement counterReducer, nameReducer, and rootReducer that compose them together.

πŸ’‘ Hints (2)
  • β€’ Each sub-reducer handles its default state independently with `state = 0` or `state = ''`.
  • β€’ The root reducer passes `state.counter` to `counterReducer` β€” it only sees its own slice.