0
試圖在我的itemsarray物業推的項目爲我的終極版減速器:如何在減速器中將項目添加到arrayproperty?
const initialState = {
items: [],
cartOpen: false,
total: 0
}
const Cart = (state = initialState, action) => {
switch (action.type) {
case 'ADD_TO_CART':
var newstate = Object.assign({}, state,
{items: [state.items, ...action.payload.found]}
);
console.log('testing=newstate', newstate);
var newTotal = 0;
console.log('testing newstate', newstate)
newstate.items.forEach(it => {
newTotal += it.price;
console.log('testing price', it.price)
});
newstate.total = newTotal;
newstate.cartOpen = true
//debugger;
return newstate;
default:
return state
}
}
export default Cart;
的action.payload.found看起來是這樣的:
{
"id":"100",
"price":10
}
我怎麼可以把這個對象的物品數組?
真棒老兄謝謝 –