我是redux的新手,我正尝试创建一个完全的redux应用程序。我遇到的问题是我的减速器不会更新我的商店。如果我要在减速器中改变商店,那么我会看到我的改变。我知道这是不好的做法,所以我正在尝试更新它而没有改变它,但是当我看着控制台时。我看不到国家的变化。有人能帮我弄清楚为什么减速机没有更新商店吗?为什么我的减速器不能更新我的商店?
这是我的行动:
store.subscribe(() => {
console.log("store changed", store.getState());
});
这里是我的减速器:
const fruitReducer = function(state={
fruits: [
{
"itemName": "banana",
"price": 1.00,
"quantityRemaining": 10
},
{
"itemName": "apple",
"price": 2.00,
"quantityRemaining": 5
},
{
"itemName": "raspberry",
"price": 5.00,
"quantityRemaining": 2
},
{
"itemName": "kiwi",
"price": 3.00,
"quantityRemaining": 15
},
{
"itemName": "pineapple,
"price": 7.00,
"quantityRemaining": 1
},
{
"itemName": "strawberries",
"price": 2.00,
"quantityRemaining": 3
}
]
}, action){
if(action.type === "DEDUCT"){
return Object.assign({}, state, {
fruits: state.fruits.map((fruit, index) => {
action.payload.map((actionFruit) => {
if(fruit.itemName === actionFruit.itemName){
let newQuantity = fruit.quantityRemaining - actionFruit.quantityRemaining;
return Object.assign({}, fruit, {
quantityRemaining: newQuantity
});
}
});
return fruit;
})
});
}
else
return state;
}
下面是我的调度员的一个例子(我创建了两个做同样的事情):
store.dispatch({type: "DEDUCT", payload: [
{
"itemName": "banana",
"quantityRemaining": 1
},
{
"itemName": "apple",
"quantityRemaining": 1
},
{
"itemName": "strawberries",
"quantityRemaining": 1
}
]});
那么我会在'action.fruits.map()'的大括号之前返回'actionFruit'?我试过了,减速器还没有更新状态。 –