1. function combineReducers(reducers) {
    2. if(!reducers) {
    3. throw new Error('reducers错误')
    4. }
    5. const reducerKeys = Object.keys(reducers)
    6. return (state = {}, action) => {
    7. // 派发动作是否引起了状态的改变
    8. let isChange = false;
    9. const nextState = {};
    10. for(const key of reducerKeys) {
    11. const prevStateKey = state[key];
    12. const reducer = reducers[key];
    13. const nextStateKey = reducer(prevStateKey, action);
    14. nextState[key] = nextStateKey
    15. isChange = isChange || nextStateKey !== prevStateKey;
    16. }
    17. return isChange ? nextState : state;
    18. }
    19. }
    20. export default combineReducers