Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

476
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda