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

107
Visualizações
scrollheight is not properly calculated until third render

I'm building a custom infinite scroll functional component in react, which can go in both directions, and am having some trouble because I need to calculate the scrollHeight and scrollWidth after the first items are loaded, which should be after the first render.

However, rather than getting the right value, I get the clientHeight even though the screen shows a scrollbar and that the size of the first child is far beyond the clientHeight, until the third render or the second effect call.

Here's the code showing the issue without the unnecessary logic, the layout effect doesn't log the full value until it ran a second time :

function InfiniteScroll({ children }) {
  const ref = useRef()
  
  console.log('render', ref?.current?.scrollHeight, children?.length)
  useLayoutEffect(() => {
    console.log('layout effect', ref?.current?.scrollHeight, children?.length)
  })

  return (
    <div ref={ref}>
      { children }
    </div>
}

Is there a way to get the full value on the first run? I'm assuming it has to do with having the render the children first to get the actual width? If that's the case, is there a way to run a useEffect hook after everything's been rendered?

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