Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Why SetState not executed on componentDidMount

I want to update a state in ComponentDidMount but the setState function is not being executed

ComponentDidMount() {
    if (this.props.location.search.includes("?token")) {
      const email = getQueryVariable(this.props.location.search, "email");
      const token = getQueryVariable(this.props.location.search, "token");
      if (this.state.showValidationModal === false) {
        console.log("Before",this.state.showValidationModal);
        this.setState({
          showValidationModal:true
        })
        console.log("After",this.state.showValidationModal);

      }
    }
  }

when the application gets mounted the result of both showValidationModal log are false

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

setState is an async operation, it doesn't update the state immediately, But the setState callback, has the last instance value from the state, So.

ComponentDidMount() {
if (this.props.location.search.includes("?token")) {
  const email = getQueryVariable(this.props.location.search, "email");
  const token = getQueryVariable(this.props.location.search, "token");
  if (this.state.showValidationModal === false) {
    console.log("Before",this.state.showValidationModal);
    this.setState({
      showValidationModal:true
    })
    // here you get the latest value of showValidationModal state
    this.setState(prevState => console.log(prevState.showValidationModal))

    // here you get the old value.
    console.log("After",this.state.showValidationModal);

  }
}
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!