码迷,mamicode.com
首页 > 其他好文 > 详细

[Redux] Implementing combineReducers() from Scratch

时间:2015-12-22 06:30:09      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }, {})
  }
};

 

[Redux] Implementing combineReducers() from Scratch

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5065444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!