简体   繁体   English

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

[英]Using Immutable JS and Redux - action is undefined

I am using Redux and Immutable JS . 我正在使用ReduxImmutable JS I set up the store like this 我这样设立了商店

import { combineReducers } from 'redux-immutable';

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

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

Now I get the following error 现在我收到以下错误

// reducers/.../initialState.js

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

TypeError: Cannot read property 'type' of undefined TypeError:无法读取未定义的属性“type”

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

When I am not using redux-immutable and set up my store like this 当我不使用redux-immutable并像这样设置我的商店

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

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

I do not get an error. 我没有收到错误。 I do not understand why it says that action.type is undefined . 我不明白为什么它说action.typeundefined Any ideas? 有任何想法吗?

I think your code should look like this 我认为你的代码应该是这样的

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

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

Example: 例:

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

Your initialState should not be a Reducer. 你的initialState不应该是Reducer。 You should only combine all your Reducers. 您应该只组合所有减速器。

Your initial State should be an object (State). 你的初始国家应该是一个object (国家)。

eg 例如

{
 loaded: true,
 someDataString: ''
}

combineReducers only takes reducers: combineReducers只需减速器:

const yourRootReducer = combineReducers({ someReducer, someOtherReducer })

It doesn't take initialState , your individual stores can (and createStore though you usually don't need to there). 不需要 initialState ,你的个人商店可以(和createStore虽然你通常不需要那里)。

Your call to createStore should be: 您对createStore的调用应该是:

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

Assuming middleware is an array. 假设中间件是一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM