mergeChildReducers(parentResult, action, childMap)

mergeChildReducers is a more generalized version of combineReducers that allows you to nest reducers underneath a common parent that has functionality of its own (rather than restricting the parent to simply passing actions to its children like combineReducers does)

NOTE: mergeChildReducers will likely be removed in a future release due to its awkward API. combineReducers and reduceReducers should cover most cases where mergeChildReducers is needed.

  • parentResult: Object | loop(Object, Cmd) – The result from the parent reducer before any child results have been applied.

  • action: Action – a redux action

  • childMap: Object<string, ReducerFunction> – a plain object map of keys to nested reducers, similar to the map in combineReducers. However, a key can be given a value of null to have it removed from the state.


import {getModel, isLoop, mergeChildReducers} from 'redux-loop';
import pageReducerMap from './page-reducers';

// a simple reducer that keeps track of your current location and nests the correct
//child reducer for that location at

const initialState = {
   location: 'index'
   //data will be filled in with the result of the child reducer

function parentReducer(state = initialState, action){
  if(action.type !== 'LOCATION_CHANGE')
     return state;

  return {...state, location: action.newLocation};

export default function reducer(state, action){
  const parentResult = parentReducer(state, action);

  let location;
    location = getModel(parentResult).location;
    location = parentResult.location;

  const childMap = {data: pageReducerMap[location]};

  return mergeChildReducers(parentResult, action, childMap);

Passing extra params

If you pass extra parameters to mergeChildReducers, they will be passed through to each nested reducer.

results matching ""

    No results matching ""