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

228
Views
How to use onChange in react-select

I am trying to create onChange function in my react-select. Here is my following code:

  const find = (event) => {
    event.preventDefault();
    if (event.target.value === "Users") {
      setSearchUsers(true);
      setSearchTeams(false);
    } else if (event.target.value === "Teams") {
      setSearchUsers(false);
      setSearchTeams(true);
    }
  };

  <Select
    className="search-select"
    instanceId="long-value-select"
    closeMenuOnSelect={false}
    components={animatedComponents}
    defaultValue="Select"
    isMulti
    onChange={(e) => find(e.target.value)}
    options={searchData}
  />

When I try to change options it's giving me this error

TypeError: event.preventDefault is not a function

How can I use onChange function in this case?

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

0

You are passing the event.target.value into the find method and then using that event value to get the event.target.value again. You do not need to do this again as you already passed the value. The prevenDefault() method does not exist on the event's value but it exists on the event object itself. Just pass the event object as the argument to use the preventDefault().

 onChange={(e) => find(e)}
about 4 years ago · Juan Pablo Isaza Report

0

Just update onChange like this:

onChange={(e) => find(e)}

or shorter:

onChange={find}
about 4 years ago · Juan Pablo Isaza Report

0

This handler only passes the value of the event to find, not the event, and the value doesn't have a preventDefault method.

onChange={(e) => find(e.target.value)}

Pass the event to the function just using the handler...

onChange={find}

...and then have the function extract the value

function find(event) {
  event.preventDefault();
  const { value } = e.target;
  if (value === 'Users') {
    // ...
  }
}
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!