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

153
Visualizações
Not getting the updated width/height on re-render

Please check the below snippet. The nested elements inside div changes and re-renders (managed by state), and accordingly height of div changes. But I'm not getting the most lasted height/width of div tag. It gives the previous (one render behind) height/width.

Any idea, how to get the most updated width/height of div on each re-render?

Thanks!

import { useRef, useEffect } from "react";

const Component = (props) => {
  const willMount = useRef(true);
  const y = useRef();
  
 // const [width, height] = useWindowSize();

  useLayoutEffect(() => {
    console.log("helloUseLayout");
    console.log(y.current.offsetWidth);
    console.log(y.current.offsetHeight);
  });

  useEffect(() => {
    if (willMount.current) {
      //...
    }
    willMount.current = false;

    console.log(y.current.offsetWidth);
    console.log(y.current.offsetHeight);
  });

  return ( 
      <div ref={y} style={{height: "300px", overflow: "auto" }}>
        // Other Nested Components...
        // causing overflow and thus creating Y-Scrollbar.
      </div> 
  );
};

export default 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