0

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?

2 Answers 2

1

setState should get an object to set:

this.setState({ validationButton: {[`button${Number}`]: true });
Sign up to request clarification or add additional context in comments.

Comments

1

I believe you can do:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.