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

112
Vistas
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
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