redux之combinreducer

为什么combinireducer可以传入很多reducer呢?

因为在经过一系列判断异常之后(第一步 浅拷贝reducer, 第二步检测reducer默认值)

用一个for循环, 获取全部的nextState,再返回,   hasChange就是判定是否state发生变化

let hasChanged = false
    const nextState = {}
    for (let i = 0; i < finalReducerKeys.length; i++) {
      const key = finalReducerKeys[i]
      const reducer = finalReducers[key]
      const previousStateForKey = state[key]
      const nextStateForKey = reducer(previousStateForKey, action)
      if (typeof nextStateForKey === 'undefined') {
        const errorMessage = getUndefinedStateErrorMessage(key, action)
        throw new Error(errorMessage)
      }
      nextState[key] = nextStateForKey
      hasChanged = hasChanged || nextStateForKey !== previousStateForKey
    }
    return hasChanged ? nextState : state

猜你喜欢

转载自www.cnblogs.com/yaooo/p/12461648.html