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

121
Visualizações
Add dynamic event listners in React

Is there any way where I can add dynamic eventlistners, which will be triggered for specific urls and it can detect which events are called on that specific url?

For ex - My react apps have multiple routes and out of those routes for specefic routes I want to see which actions (by actions I mean button click, keypress etc) are executing and based on that I want to call an API with the action that is being called.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Yes – add an event listener in an useEffect (and clean it in the effect cleanup function) in that route's component:

function MyView() {
  React.useEffect(() => {
    const handler = e => console.log(e);
    window.addEventListener('click', handler);
    return () => window.removeEventListener('click', handler);
  }, []);
  return <>Hello!</>;
}

// Not shown: hooking MyView up to your router.

You can also use a premade event hook such as react-use's 'useEvent'.

about 4 years ago · Santiago Gelvez 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