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

186
Visualizações
Does using multiple useEffects in one component have any side effects?

I used three useEffect to control different states and adding/removing event listeners. But I'm not sure that is a best practice or not?

 useEffect(() => {
    window.addEventListener('keydown', keyPressHandle)
    return () => {
      window.removeEventListener('keydown', keyPressHandle)
    }
  }, [])

  useEffect(() => {
    // do sth on state_one
  }, [state_one])

  useEffect(() => {
    // do sth on state_two
  }, [state_two])

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

0

Yes, this is a best practice and Reactjs also recommends it

Tip: Use Multiple Effects to Separate Concerns

about 4 years ago · Juan Pablo Isaza Relatório

0

As Ryan said, this is the correct way to do it.

Controlling multiple states in a single useEffect() is extremely messy and sometimes impossible depending on what you want to achieve.

Nothing in the dependency array means the code will run when the component is mounted and is the functional equivalent to componentDidMount().

When a value is in the array, the side effects trigger each time the value is updated.

As you can probably tell, using the same useEffect() for both of these things would be impossible.

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