先看一下我们在项目里面是如何使用的
使用方式:
const {combineReducers} = Redux; const rootReducer = combineReducers({ reducer1, reducer2, reducer3, ... })
//使用combineReducers的好处很明显,开发者可以按照应用状态进行分治,拆分成多个reducers,有利于开发和维护
const store = createStore(rootReducer, preloadedState, enhancer);
const combineReducers = (reducers) => { return (state = {}, action) => { //用 Object.keys取到对应的key。这个key能从state那里取到对应的模块数据 return Object.keys( reducers ).reduce((nextState, key) => { nextState[key] = reducers[key]( state[key], action ); return nextState; }, {}) } }