Javascript 存储区没有带combineReducer的有效减速机
我一直在试图找出如何在服务器端使用Javascript 存储区没有带combineReducer的有效减速机,javascript,node.js,reactjs,ecmascript-6,redux,Javascript,Node.js,Reactjs,Ecmascript 6,Redux,我一直在试图找出如何在服务器端使用combinereducer 下面是我尝试组合的两个减速机,但没有成功: 列表减速器: import ActionType from '../ActionType' export default function ListingReducer ( state = Immutable.List.of(), action){ switch(action.type) { case ActionType.ADD: ret
combinereducer
下面是我尝试组合的两个减速机,但没有成功:
列表减速器:
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(我需要将一些初始数据传递给清单缩减器,以便动态添加或删除项目):
主页_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>
}
}
《官方指南》和我的exmaple之间的主要区别在于,我先将初始状态传递给一些减速器,然后再将其传递给
组合减速器问题在于,您实际上没有将函数传递给组合减速器函数。当您执行类似于let listingStore=ListingReducer(initData.item,{})的操作时,您正在传递reducer函数的结果代码>。这将listingStore
设置为从reducer函数返回的状态,而不是reducer函数本身
如果需要动态地将初始状态传递给减速器(即,不将其硬编码到减速器中),Redux将为函数提供一个preload状态
参数
因此,与您所做的不同,您需要做如下操作:
...
let combineReducer = combineReducers({
listing: ListingReducer //function
dialog: DialogShowHideReducer //function
});
let initialState = ... // your initial state here
return createStore(combineReducer, initialState);
...
问题在于,您实际上没有将函数传递给组合减速机
函数。当您执行类似于let listingStore=ListingReducer(initData.item,{})的操作时,您正在传递reducer函数的结果代码>。这将listingStore
设置为从reducer函数返回的状态,而不是reducer函数本身
如果需要动态地将初始状态传递给减速器(即,不将其硬编码到减速器中),Redux将为函数提供一个preload状态
参数
因此,与您所做的不同,您需要做如下操作:
...
let combineReducer = combineReducers({
listing: ListingReducer //function
dialog: DialogShowHideReducer //function
});
let initialState = ... // your initial state here
return createStore(combineReducer, initialState);
...
如果我想将不同的数据传递给不同的子减速器。“createStore”如何知道数据将相应地转到哪个子减速器?它知道的方式与“知道”如何将某些操作分派给某些减速器的方式相同。您应该深入研究Redux的源代码,或者在这里阅读Dan Abramov的优秀教程:了解更多。如果我想将不同的数据传递给不同的子还原器。“createStore”如何知道数据将相应地转到哪个子减速器?它知道的方式与“知道”如何将某些操作分派给某些减速器的方式相同。您应该深入研究Redux的源代码,或者在这里阅读Dan Abramov的优秀教程:了解更多信息。
...
let combineReducer = combineReducers({
listing: ListingReducer //function
dialog: DialogShowHideReducer //function
});
let initialState = ... // your initial state here
return createStore(combineReducer, initialState);
...