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

152
Views
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
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!