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

167
Views
How awful is rendering a component in 3 different phases?

How awful is that :

  useLayoutEffect(() => {
    setWidth(ganttContainerRef.current.offsetWidth);
    setHeight(ganttContainerRef.current.offsetHeight);
  }, [])

  useLayoutEffect(() => {
    if (width > 0) {
      setGanttReady(true);
    }
  }, [width])

  useLayoutEffect(() => {
    if (ganttReady) {
      ganttRef.current.scrollTo({ left: 80 * 80 / 7 + 40 });
    }
  }, [ganttReady]);

i.e. rendering a component in 3 separate steps...

  1. One to render the container (this container is built with %age, and I need the real width for step 2 and step 3)
  2. One to render the text displayed in this container (need the text height for step 3)
  3. One to render my gantt (all with a lot of absolute positions)

If I render 1 and 2 at the same time, the container width will be 0 (first render) because 2 is not displayed yet. No matter what I do, if I do const top = refs.current[initiative.id].offsetTop;, offsetTop will have a value as if width was still 0. offsetTop is not updated with a rerender.

With the posted code above it works fine, but how awful is it? Is it doable to render a component in multiple phases or am I really hacking normal React behaviour?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

With the posted code above it works fine, but how awful is it? Is it doable to render a component in multiple phases or am I really hacking normal React behaviour?

I wouldn't say it is awful, you could say it is not very efficient, but I would not worry about it if it isn't noticeable.

You can refactor it to something like this:

  useLayoutEffect(() => {
    let offsetWidth = ganttContainerRef.current.offsetWidth;
    setWidth(offsetWidth);
    setHeight(ganttContainerRef.current.offsetHeight);

    if (offsetWidth > 0) {
      setGanttReady(true);
      ganttRef.current.scrollTo({ left: (80 * 80) / 7 + 40 });
    }
  }, []);

But the problem with this approach is as you can see it runs only on mount; which is Ok if that's what you want. So with your previous approach here:

 useLayoutEffect(() => {
    if (width > 0) {
      setGanttReady(true);
    }
  }, [width])

you had the benefit that if width changed, then you could act on it, you don't get it with my version. So it depends.

almost 4 years ago · Santiago Trujillo Report
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!