使用combineReducers合并多个reducer(基础文章)

1,447 阅读1分钟

comineReducers旨在解决什么问题?

这个函数是redux库中的函数,旨在解决多个reducer暴露的问题,因为一个组件往往用到的不止一个reducer。该函数接收的是一个对象,对象的属性分别是我们定义的reducer函数。

结合后的reducer函数

import {INCREMENT,DECREMENT} from './action-types'
import {combineReducers} from 'redux'
// 管理count状态的reducer

function count(state=1,action) {
    console.log('count',state,action);
    switch (action.type) {
        case INCREMENT:
            return state + action.data
        case DECREMENT:
            return state - action.data
        default:
            return state;
    }
}
// 管理user状态的reducer
const initUser = {};

function user(state = initUser,action) {
    switch (action.type) {
        default:
            return state;
    }
}

export default combineReducers({
    count,
    user
})

思维导图