Redux CombineReducers()无法识别状态

时间:2020-09-01 14:02:09

标签: reactjs redux reducers redux-reducers

我正在使用redux comboReducers()合并2个reducer。尽管它们在开发工具中都可以识别,但是即使我将具有不同属性的对象传递给化简器,它们中的一个也只是一个空对象。

CombineReducers()文件:

import { combineReducers } from 'redux'
import { default as JobReducer } from './Jobs/reducer'
import { default as AuthReducer } from './Auth/reducer'

const RootReducer = combineReducers({
    job: JobReducer,
    auth: AuthReducer,
})

export default RootReducer

第一个减速器:

import { SINGLE_JOB_POST_SUCCESS } from './constants'

const initial = {
    jobs: [],
    job: {}
}

export default (state = initial, action) => {
    const { type, payload } = action
    switch (type) {
        case SINGLE_JOB_POST_SUCCESS:
            return {
                ...state,
                jobs: [
                    ...state.jobs,
                    ...payload
                ]
            }
        default:
            return {}
    }
}

第二个减速器

import { RESET_AUTH_RESPONSE } from './constants'

const initial = {
    authResponse: null,
    user: {}
}

export default (state = initial, action) => {
    const { type, payload } = action
    switch (type) {
        case RESET_AUTH_RESPONSE:
            return {
                ...state,
                authResponse: null
            }
        default:
            return state
    }
}

当我在redux开发工具中查看状态时,“ auth”具有相关的属性,但是“ job”只是一个空对象。我已将化简文件称为不同的名称,以消除对别名的需要,但这没有效果。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

因为您的authReducer具有:

        default:
            return {}

因此,每当看到无法识别的动作时,它将返回一个空对象。您需要改为return state

此外,请注意,您应该切换为使用our official Redux Toolkit package,这将大大简化您的Redux逻辑。