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

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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