Irrelefant
Irrelefant

Reputation: 125

React state Toggle only works once

I want to toggle this one boolean value in the state of one of my components. This is how I'm doing it rn :

updateProduct(productID) {
this.setState((prevState) => ({
  content:{
    ...prevState.content
  },
  products:{
    ...prevState.products,
    [productID] : {  
      ...prevState.products[productID],
      editable : !prevState.editable
    }
  }
 }))
}

The problem is: This works, but only once i.e. It toggles the boolean from false to true, but then it gets stuck on being true.

Upvotes: 0

Views: 118

Answers (1)

elisecode247
elisecode247

Reputation: 119

Shouldn't it be:

editable: !prevState.products[productID].editable

Upvotes: 2

Related Questions