【发布时间】:2021-06-05 04:13:33
【问题描述】:
我正在尝试让我的 redux 使用新的表单功能,现在当我尝试显示我的主页时收到此错误消息。 index.reducer 文件最初包含我用于身份验证的教程中的 auth 和 message reducer,之前工作正常,所以我没有包含它们的 reducer。
我是用 configureStore index.reducer 文件中的五个 reducer 以正确的方式解决这个问题,还是我必须放入一个 combineReducer 函数?
错误信息
Error: Expected the reducer to be a function.
index.reducer.js
export default configureStore({
// combine the reducers
reducer: {
user: userReducer,
fields: fieldsReducer,
diveSchool: diveSchoolReducer,
auth,
message
}
});
store.js
const middleware = [thunk];
const store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(...middleware))
+ window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
export default store;
user.reducer.js
import {createSlice} from "@reduxjs/toolkit";
export const userSlice = createSlice({
name: 'user',
initialState: {
dives: [],
},
reducers: {
// expects action creator to be called with a dive object
addDive: (state, action) => {
// append to the dives array
state.dives.push(action.payload)
},
deleteDive: (state, action) => {
// append to the dives array
state.dives.push(action.payload)
}
}
})
export const { addDive } = userSlice.actions;
export const { deleteDive } = userSlice.actions;
export default userSlice.reducer;
fields.reducer.js
export const fieldsSlice = createSlice({
name: 'diveLogFields',
initialState: {
current: [],
region: [],
diveType: [],
visibility: [],
diveSpot: [],
},
reducers: {},
extraReducers: {
// picks up the success action from the thunk
// [requireData.fulfilled.type]: (state, action) => {
// // set the property based on the field property in the action
// state[action.payload.field], action.payload.items
// }
}
})
export default fieldsSlice.reducer;
【问题讨论】:
-
configureStore返回商店,您正在尝试使用它创建商店。选择一个 - 来自 redux 的createStore,或来自工具包的configureStore -
干杯,所以我收到的错误消息来自我放入 configureStore 的减速器?我需要在 configureStore 中使用 combineReducers 吗?
-
是的,需要使用
combineReducers函数将多个reducer合并为一个 -
Inside
configureStorefrom toolkit you don't need to usecombineReducers- toolkit 会为你做这件事。错误,可能来自createStore,您使用工具包不需要。