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

274
Visualizações
React: how to use useEffect compared to componentDidUpdate?

I am pretty much completely new to React. I've read the documentation and watched some YT video's. Now I am trying 'convert' some of this React class component that I found online to a functional component. This is how far I've come: My functional component(codesandbox).

It is a component to automatically wrap SVG text using tspan elements based on a user input field.

I am specifically having a hard time with the useEffect hook. As I understand it, it should function as the equivelant of componentWillMount and componentDidUpdate. Now I've got everything pretty much setup, but it does not yet update the Text component whenever new text is added in the textarea. I believe this is because I have to do something in the useEffect hook in the Text component:

  useEffect(() => {
    //something so that lines get updated if the user input changes?
  }, [lines]);

The things I've tried resulted in an infinite loop. See: Can I use set state inside a useEffect hook.

Could someone point me in the right direction? Feel free to fork the codesandbox :)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Firstly, useEffect is not like componentWillMount, it's componentDidMount - effects fire after a render.

Secondly, in your specific example, what you want is to fire an effect when dummyText updates so it can recalculate lines.

In order to do that, your effect looks like this:

  useEffect(() => {
    //something so that lines get updated if the user input changes?
  }, [dummyText]);

Thirdly, and most importantly, you shouldn't do that either because lines is not actually state of your Text component - it's just a computed value based on the dummyText prop. state is something your component owns, something it has created. Your Text component didn't create lines, it just calculated them based on the dummyText prop.

about 4 years ago · Juan Pablo Isaza Relatório

0

In your Text component your useEffect function doesn't track the dummyText prop. Try to add it into dependency array, like this (Text.jsx, line 60):

  useEffect(() => {
    const { wordsWithComputedWidth, spaceWidth } = calculateWordWidths();
    const lines2 = calculateLines(wordsWithComputedWidth, spaceWidth, 400);
    setLines(lines2);
  }, [dummyText]);
about 4 years ago · Juan Pablo Isaza 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