繁体   English   中英

ReactJS:在 state 的 object 上设置状态

[英]ReactJS: setState on object of state

我的 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))
})
}

但我收到错误消息:

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

我该怎么做才能设置为true?

setState应该得到一个 object 来设置:

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

我相信你可以做到:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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