Prakhar Prateek Prakhar Prateek - 3 months ago 12
React JSX Question

how to trigger a page change event in react js through browserhistory?

import { browserHistory } from 'react-router'


I am using browserHistory from react-router, I am looking for an event which is triggered whenever the page trasition/page change occurs through browserHistory. (This includes page navigation through back and forward of the browser too). It should have to be global. May be something like :

onNavigationChange(){
//do something
}


Looking for a solution asap.

Answer

You can use

browserHistory.listen(function(ev) {
  console.log('listen', ev.pathname);
});

<Router history={browserHistory}>{routes}</Router>

You need to use browserHistory object that you pass to ReactRouter.