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

203
Views
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 answers
Answer question

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 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!