Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda