1

I have three react-router-dom applications and I want to save browser log data from another my library which is built by node.

So how to catch react router change event from outside application??

The dependency is like this.

[A app + Logging Manager app], [B app + Logging Manager app]. [C app + Logging Manager app]

I already tried winodw.onpopstate, npm History.. but It doesn't work;;

[A App] [B App] [C App]

watching A,B,C router change event from outside

[Logging Manager App]

send logs with api call!!


here is my Logging Manager sample code

 // not working!
 window.onpopstate = function(event) {
    console.log("location: " + document.location + ", state: " + event.state);
 };


 // not working!
 const history = createBrowserHistory();
 const location = history.location;
 const unlisten = history.listen((location, action) => {
   console.log('hello ! : ', action, location.pathname, location.state);
 });

 // but window.history.state is changed!
kokojustin
  • 190
  • 3
  • 12
  • 1
    Seems like react-router is calling to `window.history.replaceState` so you can listen to it like described in that [answer](https://stackoverflow.com/a/4585031/863110). https://stackblitz.com/edit/react-router-starter-jdiry3?file=index.js – Mosh Feu Feb 06 '20 at 16:02

1 Answers1

2

Here is my answer to catch react-router change event.

const orgPushState = window.history.pushState;
window.history.pushState = function () {
   console.log('here : ', arguments);
   orgPushState.apply(this, arguments);
};
kokojustin
  • 190
  • 3
  • 12