Home > Software design >  How to update Nested object in react state?
How to update Nested object in react state?

Time:07-11

This is the current state

state={
    product:[
        {id:1, title: 'demo product 1 ', price: 10, incart: 2},
        {id:2, title: 'demo product 2', price: 11, incart: 3},
    
    
    ],
}

I have a 1 button that increase the total number of product in incart.

// the onClick handler 
// prod arguments return a product object like this {id:1, title: 'demo product 1 ', price: 10, incart: 2}

handelIncrement= (prod) => {
    
    const newState = {...this.state.product, prod}  
    this.setState({ product: newState })  // giving an error
    
  }

I appreciate your help

  • Related