Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link as a descendant of other Link causes hydration error #51694

Closed
1 task done
baunov opened this issue Jun 23, 2023 · 6 comments
Closed
1 task done

Link as a descendant of other Link causes hydration error #51694

baunov opened this issue Jun 23, 2023 · 6 comments
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@baunov
Copy link

baunov commented Jun 23, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

"next": "13.4.6"

Which area(s) of Next.js are affected? (leave empty if unsure)

Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/amazing-jennings-nyfqwl?file=%2Fapp%2Fpage.tsx%3A15%2C57

To Reproduce

I have a use case where the whole item card should be clickable Link. But also there are certain Links inside it (e.g. tags that are used to apply the search).

<Link href={{pathname: `/app/post/${item.id}`}}>
   <div>
       <h3>{item.title}</h3>
       <div>
          {item.tags.map((t) => {
              return <Link key={t} href={{pathname: '/app/search', query: {tags: s}}}>{t}</Link>
          })}
      </div>
 </div>
</Link>

Describe the Bug

Error:

Error: Hydration failed because the initial UI does not match what was rendered on the server.

Warning: Expected server HTML to contain a matching <div> in <a>.

See more info here: https://nextjs.org/docs/messages/react-hydration-error

Expected Behavior

No error. When inner Link is clicked - navigates to inner Link. When the card is clicked (outside of inner Link), navigates to outer Link.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@baunov baunov added the bug Issue was opened via the bug report template. label Jun 23, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Jun 23, 2023
@baunov baunov changed the title Link inside other Link Link inside other Link causes hydration error Jun 23, 2023
@baunov baunov changed the title Link inside other Link causes hydration error Link as a descendant of other Link causes hydration error Jun 23, 2023
@Leaden132
Copy link

getting this as well

@baunov
Copy link
Author

baunov commented Jun 26, 2023

It appears that nesting links is generally considered a bad practice (or even illegal). I solved my case by removing nesting and artificially increasing card link clickable area using css. Like in this article: https://css-tricks.com/nested-links/

@switz
Copy link

switz commented Jul 20, 2023

Also seeing this pop up quite a bit now. I suppose nested links are bad, but I have several use cases for them – anyone have any solves here?

@Laith-Alayassa
Copy link

I had the same issue.

I think the reason is that I was using a child component inside the <Link> that contained <a> tag inside it.
Once I removed the <a> tag and replaced it with a div (the navigation was controlled by the <link> now) it worked.

Not sure if that is the reason for you but that's what solved it for me. Perhaps having a <Link> as a child of <Link> causes a similar issue (I think <Link> creates an <a> tag under the hood)

@leerob
Copy link
Member

leerob commented Sep 9, 2023

This is expected – you should not nest <a> tags within <a> tags 🙏

@leerob leerob closed this as completed Sep 9, 2023
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests

5 participants