0

I am wondering if there is a way to have proper access to router via javascript. I need to redirect on certain cases, from pure javascript-based methods where I can not use things like Link components like here.

Right now, I am using a method I created:

navTo(pageHash){
    window.location.hash = pageHash; // e.g. "#settings"
    window.location.reload(true); 
}

But I would like to know if there is another elegant way, e.g. using a router component exposed to the js api.

gal007
  • 6,911
  • 8
  • 47
  • 70
  • 1
    does this answer your question? https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router – Tim Gerhard Jul 17 '20 at 18:37
  • Thank you Tim, but it doesn't work. No matter where I put the code, I always get: Error: Invalid hook call. – gal007 Jul 17 '20 at 23:01
  • 1
    do you have react-router installed? are you using a functional or class-based approach? check the other answers from this stack overflow post for different solutions – Tim Gerhard Jul 18 '20 at 07:11

0 Answers0