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

107
Views
Por qué SetState no se ejecuta en componenteDidMount

Quiero actualizar un estado en ComponentDidMount pero la función setState no se ejecuta

 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); } } }

cuando la aplicación se monta, el resultado de ambos registros showValidationModal es false

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

0

setState es una operación asíncrona, no actualiza el estado inmediatamente, pero la devolución de llamada de setState tiene el valor de última instancia del estado, entonces.

 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!