【问题标题】:Using Immutable JS and Redux - action is undefined使用 Immutable JS 和 Redux - 动作未定义
【发布时间】:2019-03-19 22:33:20
【问题描述】:

我正在使用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: 无法读取未定义的属性“类型”

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

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

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

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

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

【问题讨论】:

  • 不,我说action 是未定义的。
  • 它的动作未定义而不是 action.type。而且我认为 combineReducers() 函数需要一个减速器对象,我不确定你可以在那里传递初始状态

标签: javascript reactjs redux immutable.js


【解决方案1】:

combineReducers 只需要减速器:

const yourRootReducer = combineReducers({ someReducer, someOtherReducer })

doesn't takeinitialState,你的个人商店可以(和createStore,虽然你通常不需要在那里)。

您对 createStore 的调用应该是:

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

假设中间件是一个数组。

【讨论】:

  • const store = createStore(... 中,您使用yourRootRecuer,即您使用combineReducers(... 创建的那个,而不是rootReducer,对吧?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    例如

    {
     loaded: true,
     someDataString: ''
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2018-04-14
      • 2019-05-17
      相关资源
      最近更新 更多