Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda