question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any event/hook to detect route change?

See original GitHub issue

I have a <Router> with many <Route>

I want count each page views like Google Analytics

So the problem is how to detect Router url changing ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

109reactions
mjacksoncommented, Jan 11, 2017

@EasonWang01 It will after you hit Enter

65reactions
amxfonsecacommented, Jun 20, 2016

Can’t you use onChange or onEnter property? Just add that you your top level route(s) and it will call a function and pass you the information about the previous and the next route.

 <Route path="/" onChange={yourHandler} component={AppContainer}>
   <IndexRoute component={StaticContainer}  />
   <Route path="/a" component={ContainerA}  />
   <Route path="/b" component={ContainerB}  />
 </Route>

function yourHandler(previousRoute, nextRoute) {
   //do your logic here
}

Edit:

For this specific case maybe using the hooks on the router it’s a bit of an overkill. You can also add an event listener to your browser history if you use one.

import { browserHistory } from 'react-router';

//Your initialization

browserHistory.listen( location =>  {
 //Do your stuff here
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any event/hook to detect route change? #3554 - GitHub
I have a with many I want count each page views like Google Analytics So the problem is how to detect Router url...
Read more >
Detect Route Change with react-router - Stack Overflow
I will provide an answer once you update. That being said, the withRouter HoC is probably your best bet for making a component...
Read more >
How to detect a route change in AngularJS ? - GeeksforGeeks
In order to detect route change at any moment in AngularJS, this can be achieved by using the $on() method. The $on() method...
Read more >
AppSpec 'hooks' section - AWS CodeDeploy
The 'hooks' section for a Lambda or an Amazon ECS deployment specifies Lambda validation functions to run during a deployment lifecycle event. If...
Read more >
Event hook implementation - Okta Developer
The event hook Preview displays the status request as either successful or a failure. Check your Glitch application console. The following message should ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found