Redux combineReducers() 无法识别状态
Redux combineReducers() isn't recognising state
我正在使用 redux combineReducers() 组合 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”只是一个空对象。我已将 reducer 文件命名为不同的名称,以消除对别名的需要,但它没有效果。感谢任何帮助。
因为您的 authReducer
有:
default:
return {}
因此,只要它看到无法识别的操作,它就会 return 一个空对象。您需要将其设为 return state
。
此外,请注意,您应该改用 our official Redux Toolkit package,这将大大简化您的 Redux 逻辑。
我正在使用 redux combineReducers() 组合 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”只是一个空对象。我已将 reducer 文件命名为不同的名称,以消除对别名的需要,但它没有效果。感谢任何帮助。
因为您的 authReducer
有:
default:
return {}
因此,只要它看到无法识别的操作,它就会 return 一个空对象。您需要将其设为 return state
。
此外,请注意,您应该改用 our official Redux Toolkit package,这将大大简化您的 Redux 逻辑。