combineReducers 的作用就是把多个reducer 合并成一个,疑问createStore的时候只接受一个reducer参数
combineReducers
export default function combineReducers(reducers) { |
分解 reducerkey
var reducerKeys = Object.keys(reducers) |
因为是包含多个reducer的obj,用先把key找出来,并做基本过滤得到finalReducers
assertReducerSanity
var sanityError |
这里涉及到分支function assertReducerSanity, 也就是检查每个reducer是不是都相应的处理了初始state
和对随机type的处理
function assertReducerSanity(reducers) { |
combination
return function combination(state = {}, action) { |
返回的function shape和普通reducer保持一致 (state, action) => state
getUnexpectedStateShapeWarningMessage
开发环境下回多做一步检查:
function getUnexpectedStateShapeWarningMessage(inputState, reducers, action) { |
检查是否包含多个reducer, state的字段是否和reducer的key对应
这里就说明了 combineReducer的基本形态, key之间的对应关系
// explain the combineReducers |
state对比
var hasChanged = false |
这里对传入combineReducers的 state 每个字段(var previousStateForKey = state[key]
)都和
处理后的新state(var nextStateForKey = reducer(previousStateForKey, action)
)对比
如果state里对应的字段内容相同,hasChanged 为false
返回值
return hasChanged ? nextState : state |
调用combineReducers 后得到的就是一个state, 可能是新的,可能是传进来的那个旧的