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

207
Visualizações
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
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