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

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

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 Report

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