There seems to be some confusion with what to use over the other:
<Link to='/some/path'>
<Redirect to='/some/path'/>
history.push('/some/path')
I have been using React/Router for a little while now, and different posts/answers say different things regarding when to use these, and sometimes they don't line up with what someone else said. So I think I need some clarification on this.
From what I understand about Link
and this documentation it:
Provides declarative, accessible navigation around your application.
From what I understand about Redirect
and this documentation it:
Will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do.
It seems like all the posts I have read almost everyone uses Redirect
to navigate around there application, and no one ever recommends using Link
like in this post.
Now history
can do the same thing as Link
and Redirect
except I have a history stack trace.
Question 1: When would I want to use Link
vs Redirect
, what's the use case over the other?
Question 2: Since history
can route a user to another location in-app with the added bonus of the history stack, should I always just use the history object when routing?
Question 3: If I want to route outside of the app, what's the best method to do so? Anchor tag, Window.location.href, Redirect, Link, none of the above?
Link
makes you navigate around your app when clicking on them. WhenRedirect
is rendered, it will push an entry on thehistory
stack automatically. 2. E.g. clicking aLink
also pushes an entry to thehistory
stack, so if you can useLink
for your purposes, it is usually easier than manipulating thehistory
yourself. 3. A regular<a>
tag works great. – Tholle