原理

    1. const combineReducer = (state = {}, action) => {
    2. const newState = {
    3. loginUser: loginUser(state.loginUser, action),
    4. users: users(state.users, action)
    5. };
    6. return newState;
    7. }

    作用: 组装reducers,返回一个reducer,数据使用一个对象表示,对象的属性名与传递的参数对象保持一致

    1. import isPlainObject from './untils/isPlainObject'
    2. import ActionTypes from './untils/ActionTypes';
    3. function validateReducers(reducers) {
    4. // 判断是否是对象
    5. if (typeof reducers !== 'object') {
    6. throw new TypeError('reducers must be an object')
    7. }
    8. if (!isPlainObject(reducers)) {
    9. throw new TypeError('reducers must be a plain obect')
    10. }
    11. for (const key in reducers) {
    12. if (Object.hasOwnProperty.call(reducers, key)) {
    13. const reducer = reducers[key];
    14. let state = reducer(undefined, {
    15. type: ActionTypes.INIT()
    16. })
    17. if (state === undefined) {
    18. throw new TypeError('reducers must not return an undefined')
    19. }
    20. state = reducer(undefined, {
    21. type: ActionTypes.UNKNOWN()
    22. })
    23. if (state === undefined) {
    24. throw new TypeError('reducers must not return an undefined')
    25. }
    26. }
    27. }
    28. }
    29. export default function (reducers) {
    30. validateReducers(reducers);
    31. return function (state = {}, actions) {
    32. const newState = {}; // 返回新状态
    33. for (const key in reducers) {
    34. if (Object.hasOwnProperty.call(reducers, key)) {
    35. const reducer = reducers[key];
    36. newState[key] = reducer(state[key], actions)
    37. }
    38. }
    39. return newState;
    40. }
    41. }
    1. /**
    2. *
    3. * @param {*} obj 对象
    4. * @returns 判断是否是平面对象
    5. */
    6. function isPlainObject(obj) {
    7. if (typeof obj !== 'object') {
    8. return false;
    9. }
    10. return Object.getPrototypeOf(obj) === Object.prototype;
    11. }
    12. export default isPlainObject
    1. /**
    2. *
    3. * @param {*} length
    4. */
    5. function RandomStr(length) {
    6. return Math.random().toString(36).substr(2, length).split('').join('.')
    7. }
    8. export default {
    9. INIT() {
    10. return `@@redux/INIT${RandomStr(6)}`
    11. },
    12. UNKNOWN() {
    13. return `@@redux/PROBE_UNKNOWN_ACTION${RandomStr(6)}`
    14. }
    15. }
    • INIT和UNKNOWN是combineReducer用来检测子reducer不会返回undefined值,而默认触发的两个特殊的action
      1. for (const key in reducers) {
      2. if (Object.hasOwnProperty.call(reducers, key)) {
      3. const reducer = reducers[key];
      4. let state = reducer(undefined, {
      5. type: ActionTypes.INIT()
      6. })
      7. if (state === undefined) {
      8. throw new TypeError('reducers must not return an undefined')
      9. }
      10. state = reducer(undefined, {
      11. type: ActionTypes.UNKNOWN()
      12. })
      13. if (state === undefined) {
      14. throw new TypeError('reducers must not return an undefined')
      15. }
      16. }
      17. }