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

239
Vistas
How to get offsetTop at first loading in React?

I want to get a elements offsetTop. But it will be undefined.

How can I get it? I checked mdn.https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop

But I couldn't find how to do it.

import "./styles.css";

export default function App() {
  const dividers = document.getElementsByClassName("divider");

  const offsets = [
    dividers[0]?.offsetTop,
    dividers[1]?.offsetTop,
    dividers[2]?.offsetTop,
    dividers[3]?.offsetTop
  ];

  console.log("dividers", dividers);
  console.log("offsets", offsets);

  return (
    <>
      <p style={{ height: 200 }}>aaaaaaaaa</p>
      <div className="divider">
        _______________________________________________
      </div>
      <p style={{ height: 200 }}>bbbbbbbbb</p>
      <div className="divider">
        _______________________________________________
      </div>
      <p style={{ height: 200 }}>cccccccccccc</p>
      <div className="divider">
        _______________________________________________
      </div>
      <p style={{ height: 200 }}>ddddddddd</p>
      <div className="divider">
        _______________________________________________
      </div>
    </>
  );
}

Here is the Codesandbox.

Codesandbox

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

0

try this

import { useLayoutEffect, useRef, useState } from "react";
import "./styles.css";

export default function App() {
  const [offsets, setOffsets] = useState();
  const ref = useRef();
  useLayoutEffect(() => {
    const dividers = document.getElementsByClassName("divider");
    setOffsets(dividers);
  }, []);
  console.log(offsets);

  return (
    <>
      {offsets && (
        <>
          <p style={{ height: 200 }}>aaaaaaaaa</p>
          {offsets[0]}
          <div ref={ref} className="divider">
            _______________________________________________
          </div>
          <p style={{ height: 200 }}>bbbbbbbbb</p>
          <div ref={ref} className="divider">
            _______________________________________________
          </div>
          <p style={{ height: 200 }}>cccccccccccc</p>
          <div ref={ref} className="divider">
            _______________________________________________
          </div>
          <p style={{ height: 200 }}>ddddddddd</p>
          <div ref={ref} className="divider">
            _______________________________________________
          </div>
        </>
      )}
    </>
  );
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

I've used Array and fill to make it less repetitive and to be able to set the ref's indexes dynamically instead of hardcoding. Code sandbox

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