【发布时间】:2016-07-15 16:12:20
【问题描述】:
我一直试图弄清楚如何在official document之后在服务器端使用combineReducers。
这是我尝试组合的两个减速器,但没有成功:
ListingReducer:
import ActionType from '../ActionType'
export default function ListingReducer ( state = Immutable.List.of(), action){
switch(action.type) {
case ActionType.ADD:
return [
...state,
action.item
];
case ActionType.DELETE:
return state.filter(function(cacheItem){
return cacheItem.id !== action.item.id;
});
default:
return state
}
}
DialogShowHideReducer:
import ActionType from '../ActionType'
export default function DialogShowHideReducer ( state = false, action){
switch(action.type) {
case ActionType.DIALOG:
state = action.visible?false:true;
return state;
default:
return state;
}
}
Store.js(我需要将一些初始数据传递给listing reducer,以便动态添加或删除项目):
import {createStore} from 'redux';
import { combineReducers } from 'redux';
import ListingReducer from '../reducer/ListingReducer';
import DialogReducer from '../reducer/DialogShowHideReducer';
export default function (initData){
let listingStore = ListingReducer(initData.item,{});
let dialogStore = DialogShowHideReducer(false,{'type':'default'});
// !!!!!!No reducers coming out of this function!!!!!!!!!!
let combineReducer = combineReducers({
listing:listingStore,
dialog:dialogStore
});
return createStore(combineReducer)
}
homepage_app.js
import store from './store/Store'
import CustomComponent from './custom_component';
export default class HomePage extends React.Component {
render() {
<Provider store={store(this.props)}>
<CustomComponent/>
</Provider>
}
}
但是关于客户端页面加载的减速器失败错误是什么?
Store does not have a valid reducer.
Make sure the argument passed to combineReducers
is an object whose values are reducers.
官方指南和我的示例之间的主要区别在于,我将初始状态传递给一些减速器,然后再将它们传递给combineReducers。
【问题讨论】:
标签: javascript node.js reactjs ecmascript-6 redux