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

205
Views
How to get the exact height of the div on the first render in React?

I am using React, and I am calculating the distance/height of the div. But the values are different on the first render and rest of the renders of the component. The values coming in the second consecutive renders are correct but on the first time values are incorrect.

useEffect(() => {
      const footer = footerRef.current;
      const login = loginRef.current;

      if (!footer || !login) return;

      const windowHeight = window.screen.availHeight; // calculating the height of the screen
      const loginTop = login.offsetTop - login.scrollTop + login.clientTop // calculating the height of the div from the top
      const loginHeight = login.offsetHeight // calculating login div height
      const footerHeight = footer.clientHeight // calculating footer div height
  
      const calc = windowHeight - (loginTop + loginHeight + footerHeight) + 35;
      if (calc < 0) {
        // setting the position of the footer based on the calculation
        setCalcFooter(calc);
      }
  })

Values are different on first render and the rest.

windowHeight = 640px on both renders.
loginTop = first-render: 13px & second-render: 87px.
loginHeight = first-render: 489px & second-render: 515px.
footerHeight = first-render: 24px & second-render: 142px

<div>
   // code
   <div ref={loginRef}>
       // code
   </div>
   <div ref={footerRef} style={{ bottom: calcFooter }}> // here I am using that state variable 
      // code
   </div>
</div>

Is there any way, I can get the correct values in first render which I am getting in the second render of the component?

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!