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

122
Views
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 answers
Answer question

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 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!