我正在使用Redux和Immutable 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.type是undefined。有什么想法吗?
发布于 2018-10-15 13:43:30
combineReducers只使用减速器:
const yourRootReducer = combineReducers({ someReducer, someOtherReducer })It 不需要 initialState,您的个别商店可以( createStore,尽管您通常不需要在那里)。
您对createStore的调用应该是:
const store = createStore(
rootReducer,
initialState, // can be undefined
composeEnhancers(
applyMiddleware(...middleware)
)
);假设中间件是一个数组。
发布于 2018-10-15 13:35:26
我认为你的代码应该是这样的
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发布于 2018-10-15 13:43:02
您的initialState不应该是减速机。你应该把所有的减速机都组合起来。
您的初始状态应该是object (状态)。
(例如)
{
loaded: true,
someDataString: ''
}https://stackoverflow.com/questions/52817724
复制相似问题