Right now i need to increment/decrement a property value of state's array of objects. I have tried throwing everything that came to my mind to setState. At times it didnt throw any errors, but wasn't updating the value either Right now i get error message:
this.state.products[key] is undefined
constructor(props) {
super(props)
var products = [
{ name: "Motorhead glasses", price: 300, amount: 1 },
{ name: "Judaspriest glasses", price: 499, amount: 1 }
]
this.state = { products: products }
this.handleMinus = this.handleMinus.bind(this)
}
handleMinus(key) {
var stateCopy = Object.assign({}, this.state);
stateCopy.products = stateCopy.products.slice();
stateCopy.products[key] = Object.assign({}, stateCopy.products[key]);
stateCopy.products[key].amount += 1;
this.setState({ [this.state.products[key].amount]: stateCopy });
console.log(this)
}