首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用不可变的JS和Redux - action是未定义的。

使用不可变的JS和Redux - action是未定义的。
EN

Stack Overflow用户
提问于 2018-10-15 13:19:55
回答 3查看 411关注 0票数 1

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

代码语言:javascript
复制
import { combineReducers } from 'redux-immutable';

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

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

现在我得到以下错误

代码语言:javascript
复制
// reducers/.../initialState.js

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

TypeError:无法读取未定义的属性“类型”

它突出了switch (action.type) {

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

代码语言:javascript
复制
import { ..., combineReducers } from 'redux';    

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

我没有搞错。我不明白为什么它说action.typeundefined。有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-15 13:43:30

combineReducers只使用减速器:

代码语言:javascript
复制
const yourRootReducer = combineReducers({ someReducer, someOtherReducer })

It 不需要 initialState,您的个别商店可以( createStore,尽管您通常不需要在那里)。

您对createStore的调用应该是:

代码语言:javascript
复制
const store = createStore(
  rootReducer,
  initialState, // can be undefined
  composeEnhancers(
    applyMiddleware(...middleware)
  )
);

假设中间件是一个数组。

票数 1
EN

Stack Overflow用户

发布于 2018-10-15 13:35:26

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

代码语言:javascript
复制
export const store = createStore(
    rootReducer, initialState,
    composeEnhancers(
        applyMiddleware(...middleware)
    )
);
// I removed the combineReducers()

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

示例:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2018-10-15 13:43:02

您的initialState不应该是减速机。你应该把所有的减速机都组合起来。

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

(例如)

代码语言:javascript
复制
{
 loaded: true,
 someDataString: ''
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52817724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档