I'm new to ReactJs and I'm trying to do a simple summary basket for an ipotetic e-commerce: when you add a product you need to know if it's already there and in that case increase only the item number.
As you can see if the row doesn't exist I do a concat (row it's a object
{
id: singleProd.id,
name: singleProd.name,
price: singleProd.price,
value: 1
}
), but if exist I need to increase just the value property
state = {
counters: []
};
addCounter = row => {
let countersRows = this.state.counters;
let existRow = countersRows.filter(c => c.id === row.id);
if (existRow.length !== 0) {
let index = countersRows.findIndex(x => x.id === existRow[0].id);
console.log("Update here... like this.state.counters[index].value + 1?");
} else {
this.setState({ counters: countersRows.concat(row) });
}
};
deleteRow = counterId => {
const counters = this.state.counters.filter(c => c.id !== counterId);
this.setState({ counters });
};
I heard about Object assign, but I don't know how to do. Thanks so much.