0
我有一種情況,我想將我的減速器拆分爲多個文件,以便我可以正確管理。結合多個減速器
可以說我會用減速器做2件事。
- 只處理產品。
- 只處理聯繫人。
我可以在同一個文件中工作,但變得很難管理。
因此,我想拆分移動用戶的可讀性。
在這裏,我試圖分裂減速器的文件。
這是我將用於獲取產品的減速器之一。
module.exports.products=function(state, action) {
console.log("################################## branch_order");
console.log("reducer",state,action);
if (state==null) {
return {};
}
var new_state=$.extend(true,{},state); // XXX: deep-copy
console.log(action.type,"action type")
switch (action.type) {
case "PRODUCT_LOADING":
new_state.product_loading=true;
break;
case "PRODUCT_LOADED":
new_state.product_loading=false;
new_state.product=action.product;
break;
}
console.log("new_state",new_state);
return new_state;
}
這裏是另一個將存儲聯繫人的減速器。
module.exports.contacts=function(state, action) {
console.log("################################## pawn_order",state);
console.log("reducer",state,action);
if (state==null) {
return {};
}
return state;
}
這是怎麼了我正在結合減速。
var product = require("./prodcut")
var contact = require ("./contact")
var combineReducers = require ('redux').combineReducers
module.exports = combineReducers({product,contact});
這就是我在我的主文件中調用並將其傳遞給存儲。
var reducers = require ("./reducer") /// <<<<< this is how i call
var RPC = require('./RPC')
//var actions = require("./actions")
var createStoreWithMiddleware=applyMiddleware(thunkMiddleware)(createStore);
var store=createStoreWithMiddleware(reducers);
ReactDOM.render(<Provider store={store}><Index/></Provider> , document.getElementById('content'))
我試圖按照combine reducers的教程,但遇到了一些問題。
這種組合似乎工作。
出了什麼問題?
當第一減速器被稱爲(產品),它的工作原理 當第二減速器被稱爲(接觸)整個狀態似乎被替換
我想將數據追加的狀態(我不知道如何做到這一點請大家幫忙)
只是爲了信息,這是我怎麼罵我接觸的狀態數據:
var select=function(state){
return {
product_loading:state.product_loading,
product:state.product,
}
}
這就是我如何撥打我的組件
render:function(){
if (!this.props.product_loading) return <div> Loading</div>
在此先感謝。期待一些解決方案。
你可能會在一起工作的例子,所以我們可以玩它嗎? (例如在jsbin上) –
因此,reducer正在分離的狀態樹上工作,這意味着每個reducer都有自己的位和整個狀態的bob。 因此,您不會看到「全局」狀態(如從另一個減速器中看到的那樣),但是您必須複製您當前減速器中感興趣的狀態。 –