[Redux] Implementing combineReducers() from Scratch

The combineReducers function we used in previous post:

const todoApp = combineReducers({
  todos,
  visibilityFilter
});

 

  • It accepts and object as agruement;
  • It returns an function

 

Implemente by ourself:

 // reducers: {todos: todos, filter: filter}
const combineReducers = (reducers) => {
   // return a reducer function
  return (state={},action)=>{
     // combine the reducers
    return Object.keys(reducers)
      .reduce( (acc, curr)=>{
        acc[curr] = reducers[curr](
          state[curr],
          action
        ); // todos: todos
      
      return acc;
    }, {})
  }
};

 

posted @ 2015-12-22 02:57  Zhentiw  阅读(224)  评论(0编辑  收藏  举报