简体   繁体   中英

ReactJS: setState on object of state

I have in my state:

state = {
  validationButton: {
   button1: false
   }
}


componentDidMount(){
//....
this.check(1)
this.check(2)
}

check(Number){
 let today = new Date()
 // I call my api to see if exists document with the number
  db.get(`${Number}:${today}`)
  .then( (doc) => {
    //after that I found document:
    // * this is what I would to do *
    this.setState((this.state.validationButton[`button${Number}`] = true))
})
}

But I receive the error message:

Invariant Violation: setState(...): takes an object of state variables to update or a function which returns an object of state variables.

How can I do to set to true?

setState should get an object to set:

this.setState({ validationButton: {[`button${Number}`]: true });

I believe you can do:

this.setState({ validationButton: {[`button${Number}`]: true });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM