reactjs - 如何在没有初始状态的情况下使用 combineReducers 创建 Redux 存储
问题描述
我有以下 Redux 商店:
import {createStore} from 'redux';
import rootReducer from './reducers';
export function configureStore() {
const store = createStore(rootReducer);
return store;
};
const store = configureStore()
export default store;
这是rootReducer
创建的combineReducers
:
import {combineReducers} from 'redux';
import application from '../features/application/reducers';
const rootReducer = combineReducers({
application,
});
export default rootReducer;
这是提供者的创建:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import store from './app/store';
import { Provider } from 'react-redux';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
问题是我收到以下错误:
Error: The slice reducer for key "application" 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.
我检查了此文档,但找不到解决问题的方法。
编辑
我看到问题可能与 webpack 有关,但我不知道:
这是application
reducer的代码:
import { ActionInterface } from '../generals';
import {
FETCH_APPLICATION_COMPOSITE_SUCCESS,
SET_CURRENT_APPLICATION_COMPONENT
} from './actions';
const INIT_STATE = {
applicationComposite: null,
currentApplicationComponent: null
}
export default (state=INIT_STATE, action: ActionInterface) => {
switch(action.type) {
case FETCH_APPLICATION_COMPOSITE_SUCCESS: {
return {
...state,
//@ts-ignore: Object is possibly 'undefined'
applicationComposite: action.payload.applicationComposite
}
}
case SET_CURRENT_APPLICATION_COMPONENT: {
return {
...state,
//@ts-ignore: Object is possibly 'undefined'
currentApplicationComponent: action.payload.applicationComponent
}
}
}
}
解决方案
您需要将默认返回添加到减速器
import { ActionInterface } from '../generals';
import {
FETCH_APPLICATION_COMPOSITE_SUCCESS,
SET_CURRENT_APPLICATION_COMPONENT
} from './actions';
const INIT_STATE = {
applicationComposite: null,
currentApplicationComponent: null
}
export default (state=INIT_STATE, action: ActionInterface) => {
switch(action.type) {
case FETCH_APPLICATION_COMPOSITE_SUCCESS: {
return {
...state,
//@ts-ignore: Object is possibly 'undefined'
applicationComposite: action.payload.applicationComposite
}
}
case SET_CURRENT_APPLICATION_COMPONENT: {
return {
...state,
//@ts-ignore: Object is possibly 'undefined'
currentApplicationComponent: action.payload.applicationComponent
}
}
default: return state;
}
}
推荐阅读
- r - 如何按日期对客户订单进行分组
- javascript - 如何在 ngFor 循环中使用 ngb-accordion?
- ajax - Woocommerce 使用 ajax 对购物车的总价应用折扣
- laravel - 如何在 Laravel 7 中运行普通查询,而不是使用 Eloquent
- java - Palindrome 程序适用于单个测试用例,但在其他情况下继续输入
- c++11 - 如何删除文件并避免引入竞争条件?
- html - 组合 2 个 html 文件时,html 中的多个脚本标签不起作用
- c# - 我需要检查整数中的每个数字在c#中是偶数还是奇数,返回偶数或奇数
- image - 由于ocr的行删除,填补了汉字的空白
- asp.net-core - 将 WebApi 托管为 IIS 应用程序,以便它运行启动类