0

我正在使用 redux combineReducers() 来组合 2 个减速器。虽然它们都在开发工具中被识别,但其中一个只是一个空对象,即使我将具有不同属性的对象传递给减速器。

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”只是一个空对象。我已经为减速器文件命名了不同的名称,以消除对别名的需求,但它没有任何效果。任何帮助表示赞赏。

4

1 回答 1

0

因为你authReducer有:

        default:
            return {}

因此,只要它看到一个它无法识别的动作,它就会返回一个空对象。你需要它来return state代替。

另外,请注意您应该改用我们的官方 Redux Toolkit 包,这将大大简化您的 Redux 逻辑。

于 2020-09-01T14:39:50.483 回答