使用 Immutable JS 和 Redux - 动作未定义

Using Immutable JS and Redux - action is undefined

我正在使用 ReduxImmutable JS。 我这样设置商店

import { combineReducers } from 'redux-immutable';

...
const rootReducer = combineReducers({});    
import { initialState } from '../reducers/...';

export const store = createStore(
    combineReducers(rootReducer, initialState),
    composeEnhancers(
        applyMiddleware(...middleware)
    )
);

现在我得到以下错误

// reducers/.../initialState.js

export function foo(state = initialState, action) {
    switch (action.type) {
     ...
...

TypeError: Cannot read property 'type' of undefined

并突出显示 switch (action.type) {

当我不使用 redux-immutable 并像这样设置我的商店时

import { ..., combineReducers } from 'redux';    

export const store = createStore(
    combineReducers({ initialState }),
    composeEnhancers(
        applyMiddleware(...middleware)
    )
);

我没有收到错误。我不明白为什么说 action.typeundefined。有什么想法吗?

我认为你的代码应该是这样的

export const store = createStore(
    rootReducer, initialState,
    composeEnhancers(
        applyMiddleware(...middleware)
    )
);
// I removed the combineReducers()

参考:https://github.com/gajus/redux-immutable#usage

示例:

import {
  combineReducers
} from 'redux-immutable';

import {
  createStore
} from 'redux';

const initialState = Immutable.Map();
const rootReducer = combineReducers({});  // we use the combineReducers() here
const store = createStore(rootReducer, initialState); // and don't need to use it here

您的 initialState 不应该是 Reducer。 您应该只组合所有的 Reducer。

您的初始状态应该是 object(状态)。

例如

{
 loaded: true,
 someDataString: ''
}

combineReducers 只接受减速器:

const yourRootReducer = combineReducers({ someReducer, someOtherReducer })

doesn't take initialState,您的个人商店可以(和 createStore 虽然您通常不需要去那里)。

您对 createStore 的调用应该是:

const store = createStore(
  rootReducer,
  initialState, // can be undefined
  composeEnhancers(
    applyMiddleware(...middleware)
  )
);

假设中间件是一个数组。