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

202
Views
function returning opposite boolean value to what I expected

I have this function in my .tsx file:

doIsChecked() {
    var isItChecked = document.getElementById('isPless') as HTMLInputElement;

    console.log('updateDocsFlag before: ' + this.state.updateDocsFlag);
    if (isItChecked.checked) {
        this.setState({ updateDocsFlag: true });
    } else {
        this.setState({ updateDocsFlag: false });
    }
    console.log('updateDocsFlag after: ' + this.state.updateDocsFlag);
}

It get called when a checkbox is ticked/unticked. I noticed that if isItChecked is true updateDocsFlag is false as can be seen in the before and after log entries. This isn't the behaviour I expected Is there something wrong with my code?

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

0

this.setState is asynchronius. it does set the right state,but not at the moment of the call. so when you call

    console.log('updateDocsFlag after: ' + this.state.updateDocsFlag);

you still see the old state. try to do following:

doIsChecked() {
    var isItChecked = document.getElementById('isPless') as HTMLInputElement;

    console.log('updateDocsFlag before: ' + this.state.updateDocsFlag);
    let updateDocsFlag = isItChecked.checked;
    this.setState({ updateDocsFlag });
    console.log('updateDocsFlag after: ' + updateDocsFlag);
}

this way you log not the state, but the value you passing into the state. You suppose to see there the right value. (and the actual state will update with this value after all active functions traces will end)

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!