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

337
Views
Function doesn't accept updated value React JS

ascendingDirectionSort has 'true' initial value. It can be changed by dropdownlist. When it is changed, console.log in the sortData function shows updated value, but only the ascendingDirectionSort==true condition executes, even when it's false.

   ...Class
   ...constructor
this.state={
  ascendingDirectionSort:true
}
render()...
const {ascendingDirectionSort}=this.state


const sortData(field)=>{
  console.log(ascendingDirectionSort)//true, when ascendingDirectionSort=true and false when = false
  if(ascendingDirectionSort){
  execute1...//always executes
  console.log(ascendingDirectionSort)
  }else{
  execute2...//doesnt work
  
  }

}
...
   <select value = {ascendingDirectionSort} onChange={this.handleChange}>
        <option value={true}>Ascending</option>
        <option value={false}>Descending</option>
    </select>
...

handleChange = (event) => {
  this.setState({ ascendingDirectionSort: event.target.value });
};

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

0

<option value={true}>

While you are passing boolean values to the value props, they will be rendered as HTML attributes and so will be converted to strings.

if(ascendingDirectionSort){

You are testing the truthiness of the value of that variable.

true, "true" and "false" are all true values.


Use strings consistently.

this.state={
  ascendingDirectionSort:"true"
}

and

if(ascendingDirectionSort === "true"){
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!