javascript - 使用combineReducers "Error: Reducer "assetReducer时出错“初始化期间返回未定义”。

标签 javascript reactjs typescript redux

Error: Reducer "assetsReducer" 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.

在我的 ReactJS 应用程序中,我有 2 个 reducer /操作(assetsboard)。

在我的 store.ts 文件中,我使用 combineReducers ,如下所示:

import { applyMiddleware, createStore, combineReducers } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'

import { IinitialState } from './shared/types'
// import reducer from './reducers/assets/'
import assetsReducer from './reducers/assets'
import boardReducer from './reducers/board'

const rootReducer = combineReducers({
  assetsReducer,
  boardReducer
});

export const defaultInitialState = {
  assets: [],
  portfolio: [],
  loading: false,
  overlay: false
};

console.log('rootReducer', rootReducer);

export function initializeStore(initialState: IinitialState = defaultInitialState) {
  return createStore(
    rootReducer,
    initialState,
    composeWithDevTools(applyMiddleware(thunkMiddleware))
  )
}

这应该是正确的,但是当我在本地主机上刷新我的应用程序时,我收到以下 typescript 错误和上面的控制台错误。

Type 'IinitialState' has no properties in common with type 

'DeepPartial<{ assetsReducer: { assets: any; loading: boolean; portfolio: never[]; overlay: boolean; }; boardReducer: { overlay: any; assets: never[]; portfolio: never[]; loading: boolean; }; }>'.ts(2559)

enter image description here


assetsReducer:

import { Actions } from '../actions/assets'
import { defaultInitialState } from '../store'

export default (state = defaultInitialState, action: any) => {
  switch (action.type) {
    case Actions.GET_ALL_ASSETS: {
      const { assets } = action;
      return {
        ...state,
        assets,
        loading: false
      };
    }

    default:
      return state;
  }
};

boardReducer

import { Actions } from '../actions/board'
import { defaultInitialState } from '../store'

export default (state = defaultInitialState, action: any) => {
  switch (action.type) {
    case Actions.SET_OVERLAY_STATE: {
      const { overlay } = action;
      return {
        ...state,
        overlay
      };
    }

    default:
      return state;
  }
};

最佳答案

您的初始状态不正确。您的初始状态应如下所示:

export const defaultInitialState = {
  assetsReducer: {assets: [], loading: false, portfolio: [], overlay: false},
  boardReducer: {assets: [], loading: false, portfolio: [], overlay: false},
}

有关这方面的更多信息可以在 Redux Docs 中找到。

关于javascript - 使用combineReducers "Error: Reducer "assetReducer时出错“初始化期间返回未定义”。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54775023/

相关文章:

javascript - 如何修复(错误:any)=>Observable<any> is not assignable?

json - 如何从 typescript 中的json响应中获取日期对象

reactjs - 如何在运行时通过docker-compose.yaml文件传递的React应用中读取环境变量?

angular - 无法从我的 Controller /构造函数访问输入

javascript - JavaScript insideHTML 到图像替换的问题

javascript - infobox.prototype = new google.maps.overlayview() 错误,google未定义

reactjs - 在 create-react-app 上运行构建后出现空白页面

javascript - 如何比较两个数组的值,如果两者相等或不返回差异。按钮

javascript - 需要javascript检查夏威夷阿拉斯加等的邮政编码

javascript - 如何检测表单中的输入按键并滚动到元素?