function combineReducers(reducers) {
if(!reducers) {
throw new Error('reducers错误')
}
const reducerKeys = Object.keys(reducers)
return (state = {}, action) => {
// 派发动作是否引起了状态的改变
let isChange = false;
const nextState = {};
for(const key of reducerKeys) {
const prevStateKey = state[key];
const reducer = reducers[key];
const nextStateKey = reducer(prevStateKey, action);
nextState[key] = nextStateKey
isChange = isChange || nextStateKey !== prevStateKey;
}
return isChange ? nextState : state;
}
}
export default combineReducers