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

 

posted @ 2020-03-11 12:42  会挽雕弓如满月的张  阅读(241)  评论(0)    收藏  举报