key "pageName" 的 slice reducer 在初始化期间返回 undefined
slice reducer for key "pageName" returned undefined during initialization
我正在使用 createSlice() 创建 redux 应用程序,但出现错误:
Error: The slice reducer for key "pageName" returned undefined during
initialization. If the state passed to the reducer is undefined, you
must explicitly return the initial state. The initial state may not be
undefined. If you don't want to set a value for this reducer, you can
use null instead of undefined.
这是此错误的最小示例。
pageNameSlice.js:
import { createSlice } from "@reduxjs/toolkit";
const pageNameSlice = createSlice({
name: 'pageName',
initalState: "",
// The `reducers` field lets us define reducers and generate associated actions
reducers: {
setPageName: (state, newName) => {
state.pageName = newName
}
}
});
export default pageNameSlice.reducer;
store.js:
import { configureStore } from '@reduxjs/toolkit';
import pageNameReducer from '../features/pageNameSlice';
export const store = configureStore({
reducer: {
pageName: pageNameReducer
},
});
你打错了 - initalState
应该是 initialState
。 (这比你想象的更常见^^)
我正在使用 createSlice() 创建 redux 应用程序,但出现错误:
Error: The slice reducer for key "pageName" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.
这是此错误的最小示例。
pageNameSlice.js:
import { createSlice } from "@reduxjs/toolkit";
const pageNameSlice = createSlice({
name: 'pageName',
initalState: "",
// The `reducers` field lets us define reducers and generate associated actions
reducers: {
setPageName: (state, newName) => {
state.pageName = newName
}
}
});
export default pageNameSlice.reducer;
store.js:
import { configureStore } from '@reduxjs/toolkit';
import pageNameReducer from '../features/pageNameSlice';
export const store = configureStore({
reducer: {
pageName: pageNameReducer
},
});
你打错了 - initalState
应该是 initialState
。 (这比你想象的更常见^^)