Kindaa confuse in usage of both and i am using both of them in redirecting. 😬
For further actions, you may consider blocking this person and/or reporting abuse
Kindaa confuse in usage of both and i am using both of them in redirecting. 😬
For further actions, you may consider blocking this person and/or reporting abuse
John Vester -
jin -
Edgaras -
keploy -
Top comments (2)
I guess the easy answer is
Use
<Link href="" passHref>
when you can define the destination in JSX.And use
useRouter
when you want to programmatically redirect using javascript.eg. instead of using
window.location = ""
userouter.push("")
Hi @rishabh0906, I'll try to answer your question, but before that, let me say some useful information.
<Link>
tags are the best option if they can be used, as they have a bunch of nice features like prefetching the page when the link is in the viewport.As per your question,
<Link>
tags have to be used every time you would put a link on a page (e.g. with an<a>
tag), whilerouter.push()
have to be used whenever a<Link>
tag can't, for example, if you have to redirect the user after some complex logic.Example
An example of link usage:
An example of
useRouter
usage:Caveats
The
<Link>
tag has to be used only for internal navigation. if you have to redirect the user to an external page, simply use an<a>
tag.