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

178
Vistas
Is it possible to pass an id/ string to an onFocus event?

is it possible to pass the the key/ID to a onFocus event?

Here's my code:

  handleChange: (e: SyntheticEvent<FormInputElements>) => void,
  handleFocus: (e: SyntheticEvent<FormInputElements>) => void,

  const handleEvent = (event?: EventListener, inputvalue: string) => {
    if (event) {
      return event(component, inputvalue);
    }
    return null;
  };

  const handleChange = (e: SyntheticEvent<FormInputElements>) => {
    handleEvent(onChange, e.currentTarget.value);

    if (hasFocus && showErrors) {
      setResolvedErrors(true);
      handleFocus(component.key);
    }
  };

  const handleFocus = (e: SyntheticEvent<FormInputElements>) => {
    setHasFocus(true);
    handleEvent(onFocus, e.currentTarget.value);
  };

Basically, within my handleChange event - if the if statement is ran - I'd like to then run the handleFocus event - but pass it the key of my component, so that it focuses on the correct element?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use useRef hook for that purpose.

Have a look at the given example:

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}

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