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

474
Views
how to access event as well as value passed to function in react

how do i access event and value passed to function at a time here is what i have tried

   <ul className="list" data-testid="lists">
              {options.map((option, i) => (
                <li
                  role="button"
                  tabIndex="0"
                  onClick={() => {
                    setSelected(option);
                    setOpen(false);
                  }}
                  className={state.cursor === i ? "activeList" : "list-item"}
                  onKeyDown={ handleEnter(option) }
                >
                  {option}
                </li>
              ))}
            </ul>

  const handleEnter = (option, e) => {
    if (e.keyCode === 13) {
      setSelected(option)
    }
  };

what i want to do is when enter pressed want to update the state with value of option but not able to do so what should i do

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

0

The event object is passed into the onXXX function.

If you want to pass it from the onKeyDown function to the handleEnter function then you must do so explicitly.

You also have to pass a function to onKeyDown in the first place, not undefined which is the return value of the function you are calling.

Use useCallback to stop the function being recreated on every render.

const onKeyDown = useCallback(
    (event) => handleEnter(option, event),
   [option, handleEnter]
);

and

onKeyDown={ onKeyDown }

Do note, however, that keyCode is deprecated and we have better alternatives for it now.

about 4 years ago · Juan Pablo Isaza Report

0

You can update the property like

onKeyDown={(e)=> handleEnter(option, e)}

it will bind your events in your handleEnter function. I hope it will work for you. Thanks.

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!