簡體   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