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?
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.
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.
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With