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

111
Views
How to attach id on ref or store refs by ID in react?

How to avoid attaching id attributes to all of the elements, instead I want to keep ids within refs.

I have list of refs

  const questionsRef = useRef<Array<HTMLDivElement | null>>([]);

I am looping through questions and attach on every question ID, which later I will use to scroll to that question.

Is there a way to avoid attaching ID to the element? To attach it on ref or to save refs by element ID something like that?

 <div
    id={elementId}
    ref={el => {
    questionsRef.current[rflxIdx] = el;
    }}
 >

Cus later I am scrolling to that question if is not answered

export const focusNextQuestion = (
  questionsRef: React.MutableRefObject<(HTMLDivElement | null)[]>,
  nextQuestion: Readonly<Question | null>,
) => {
  if (questionsRef.current) {
    const elementId =
      nextQuestion?.data?.base?.group?.name || nextQuestion?.data?.base?.key;

    const focusElement = questionsRef.current.find(el => el?.id === elementId);

    if (focusElement) {
      focusElement.scrollIntoView({
        behavior: 'smooth',
        block: 'center',
        inline: 'nearest',
      });

      focusElement?.classList.add('focus-unanswered-question');

      Promise.all(
        focusElement.getAnimations().map(animation => animation.finished),
      ).then(() => focusElement?.classList.remove('focus-unanswered-question'));
    }
  }
};

about 4 years ago · Juan Pablo Isaza
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!