...

/

A Closer Look at Reducers

A Closer Look at Reducers

Learn about the reducers in depth.

We'll cover the following...

If you open the reducers/root.js file, you will find that the same reducer is now taking care of different parts of our state tree. More properties will be added to both the recipes and the ingredients subtrees as our application grows. Since the code in both handlers is not interdependent, we can split it further into three reducers, two that are responsible for different parts of the state and one to combine them:

const recipesReducer = (recipes, action) => {
  switch (action.type) {
    case 'ADD_RECIPE':
      return recipes.concat({name: action.name});
  }

  return recipes;
};

const ingredientsReducer = (ingredients, action) => { ... }

const rootReducer = (state, action) => {
  return Object.assign({}, state, {
    recipes: recipesReducer(state.recipes, action),
 
...