Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.history vs. Router.history to go back

Tags:

reactjs

Why would I ever use the Router history from react-router-dom over window.history.back()?

And is there a way of detecting if a user came from one of the pages in my app or from a specific external website and redirect him according to that?

like image 793
Gambit2007 Avatar asked Feb 05 '19 17:02

Gambit2007


People also ask

How do I go back in history response?

To go back to the previous page, pass -1 as a parameter to the navigate() function, e.g. navigate(-1) . Calling navigate with -1 is the same as hitting the back button.

How do I use .push history?

Using history.push() is another approach where we make use of the history props React Router provides while rendering a component. In other words, this works when the component is being rendered by React Router, bypassing the component as a Component prop to a Route.


1 Answers

Because. window.history.back() will just change the url in the url bar and reload the application and all the resources again but not rerender the correct UI component while Router history from react-router-dom sets the url plus render the correct React component based on the route.

Second Part: No. You can't detect from which external website user came to your app from as because of the Security Reasons More.

While in your own app Yes. you can always detect if user navigated from one of the pages of your app by window.onpopstate event usage .

like image 102
Gaurav Saraswat Avatar answered Sep 23 '22 18:09

Gaurav Saraswat