0

I have a requirement to listen/detect the Location URL change in React application.

I have tried using below code but it changes the location and then gets called.

const location = useLocation();

  useEffect(() => {
    debugger; 
    console.log('Location changed');
  }, [location]);

Is there a way to listen a URL change before actually changing the location?

Also, Is it possible to find "fromLocation" and "ToLocation"?

I am using "react-router-dom": "^5.2.0".

Please help.

Thanks & Regards, Jyoti

VMT
  • 789
  • 1
  • 3
  • 11
Jyoti
  • 2,065
  • 3
  • 25
  • 28
  • 2
    "Is there a way to listen a URL change before actually changing the location?" If the location hasn't changed yet what location change are you expecting to "listen" for? Does this help answer your question? https://stackoverflow.com/a/70646548/8690857 – Drew Reese May 27 '22 at 05:51
  • Hi, thanks for the response . But we have used old version of react-router-dom. Is there any approach for old version? – Jyoti May 27 '22 at 07:04
  • 1
    If by "old version" you mean `react-router-dom@5`, it has a peer dependency on `history@4` and allows you to use a custom `history` object. The `history` object API hasn't changed significantly. – Drew Reese May 27 '22 at 07:07

0 Answers0