-1

I hope everyone is fine. I am going through a tough situation that's why I came here for Help I am using react with class components. I am trying to move to next page using windows.location.href but when I come back to previous page It shows "Cannot GET /Route" this is my App.js file App.js File Where I defining My Routes

When I User it works fine error does not appears . but when I programmatically go to next route is appears. Is there any other way to move

  • You should use the history object : https://reactrouter.com/web/api/history for this purpose – Imanpal Singh Jul 17 '21 at 06:21
  • Do you ***need*** to use `windows.location.href` for navigation instead of the react-router-dom methods for navigation, i.e. `Links` and using the `history` object? Using `windows.location.href` is reloading your app. – Drew Reese Jul 17 '21 at 06:21
  • I am using the class component. How Can I Use UseHistory inside class component it shows error – Hafiz Muhammad Atif Jul 17 '21 at 06:59

2 Answers2

1

Probably the largest benefit of React Router is that we can simulate vising different pages while being a single page application, so we don't have to wait on a page refresh. With that in mind, it's in your best interest to use useHistory.

There's a similar post about accessing useHistory in a class based component here

sjmunro
  • 91
  • 4
0

If you are calling this function through a submit button. This may be the reason why the browser does not redirect. It will run the code in the function and then submit the page instead of redirect. In this case change the type tag of your button.

saqib kafeel
  • 296
  • 3
  • 8