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

229
Views
onMouseDown event in react does not trigger state changes but onClick does

JSX part looks like this:

<span className="header_srh_i_c" onMouseDown={this.toggleSearchbar}>
    <i className="fa fa-search"
        id="searchButton"
        name="searchButton"
     />
 </span>

function in the same component looks like this:

toggleSearchbar(event){
    const eventType = event.type;
    if(this.state.showSearch && this.props.q && length(this.props.q) > 0){
      this.toggleSearching();
    }else{
      console.log("state after", this.state.showSearch)  //false

      this.setState({showSearch:!this.state.showSearch},function (event) {
      console.log("state after", this.state.showSearch) //false
      })
    }
  }

state remains false event after execution of

this.setState({showSearch:!this.state.showSearch},function (event) {
          console.log("state after", this.state.showSearch) //false
          })

However, it works just fine if I use onClick instead of onMouseDown

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

try

this.setState(prev => ({showSearch: !prev.showSearch}), function(){.....})

instead of

this.setState({showSearch:!this.state.showSearch},function (event) {
      console.log("state after", this.state.showSearch) //false
      })
over 4 years ago · Santiago Trujillo 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!