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

204
Vistas
ReactJS: How to autofocus inputs in a slider

I have an application like quiz. Every single slider has one input. I want to autofocus the related input when the user clicks on the next button or press on the enter.

I tried to achieve it by using useEffect. However, it doesn't work.

This is the related function which includes the next function of slick and focus attribute of inputs:

const inputRef = useRef([]);

const handleEnter = (event, index) => {
      if (event.key.toLowerCase() === "enter") {
          sliderRef.current.slickNext(); // The next slider function of the react-slick package
          inputRef.current[index].focus()
      }
}

Here are my inputs in map function:

{data.map((post, index) => (
  <Input 
     id={post.question_uuid} 
     name={post.question_uuid} 
     onKeyDown={e => {handleEnter(e, index)}}
     autoComplete="off
     ref={el => inputRef.current[index] = el}
  />
))}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use useRef for every input and store them on an array with the index that came from your map then after changing every slide you can get the input ref from the array based on the current index and call array[index].current.focus()

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