Why do I need to pass 'as' to Link on dynamic routes? #13372
-
Hi everyone! I'm having a lot of trouble understanding why I would need to pass the props In my basic app I'm basically mapping over an array of users and returning a Link for each user: {users.map(user => {
return (
<Link href={`/users/${user.id}`} ...>
<a>
<p>Name: {user.name}</p>
<p>Email: {user.email}</p>
</a>
</Link>
)
})} I built this a couple of weeks ago, and it worked as I expected. No warnings on console, my app was transitioning through routes normally. However I was re-reading the docs and according to https://nextjs.org/docs/api-reference/next/link, I should be doing this... {users.map(user => {
return (
<Link href={`/users/[id]`} as={`/users/${user.id}`} ...>
<a>
<p>Name: {user.name}</p>
<p>Email: {user.email}</p>
</a>
</Link>
)
})} I read that But, could someone elaborate a little bit regarding this? Why if I ignore the Thanks!! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
TL;DR Next.js doesn't ship a manifest of routes to the client, so it doesn't know that
It can be confusing, but we'll be working on improving this 😌 |
Beta Was this translation helpful? Give feedback.
TL;DR Next.js doesn't ship a manifest of routes to the client, so it doesn't know that
/users/${user.id}
has to use the page/users/[id]
.Link
is only used for page navigations, so thehref
attribute is used to select the page, which is usually the same path that's going to be in the URL, when that's not the case is whenas
is required, to lethref
select the page and thenas
will select the path.It can be confusing, but we'll be working on improving this 😌