increaseOrderQuantity(item) {
const itemIndex = this.state.data.indexOf(item);
this.setState(({data}) => ({
data: [
...data.slice(0, itemIndex),
{
...item,
quantity: item.quantity + 1
},
...data.slice(itemIndex + 1) // have made a mistake of forgetting to include + 1
]
}));
}
With immer, less error-prone:
import produce from 'immer';
.
.
.
increaseOrderQuantity(item) {
const itemIndex = this.state.data.indexOf(item);
this.setState(produce(draft => {
draft.data[itemIndex].quantity++;
}));
}
No comments:
Post a Comment