最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NextJS Link isn't rendering an anchor tag - Stack Overflow

programmeradmin1浏览0评论

I'm using React + NextJS, and I'm trying to render a list of products, much like you'd see on a typical emerce category page. Each product is in a p, and that p should link to the appropriate detail page (so it should be surrounded by an anchor a tag).

Unfortunately, the links work but they don't render actual anchor tags. Can anyone explain what I'm missing?

Scenario A: a normal text link (works as expected)


input:

<Link href="wherever">word holmes</Link>

output:

<a href="wherever">word holmes</a>

Scenario B: linking a block of whatever


input:

<Link href="wherever">
    <p>word holmes</p>
</Link>

output:

<p>word holmes</p>

desired output:

<a href="wherever"><p>word holmes</p></a>
  • where'd the <a /> tag go??
  • mousing over the block does not get a pointer cursor, but clicking it does send you to the href target

Scenario C: adding my own a to the block of whatever


input:

<Link href="wherever">
    <a>
        <p>word holmes</p>
    </a>
</Link>

output:

<a href="wherever"><p>word holmes</p></a>
  • ok this works, but it feels hacky..
  • why does this work? Is this the "right" way to do it?

I'm using React + NextJS, and I'm trying to render a list of products, much like you'd see on a typical emerce category page. Each product is in a p, and that p should link to the appropriate detail page (so it should be surrounded by an anchor a tag).

Unfortunately, the links work but they don't render actual anchor tags. Can anyone explain what I'm missing?

Scenario A: a normal text link (works as expected)


input:

<Link href="wherever">word holmes</Link>

output:

<a href="wherever">word holmes</a>

Scenario B: linking a block of whatever


input:

<Link href="wherever">
    <p>word holmes</p>
</Link>

output:

<p>word holmes</p>

desired output:

<a href="wherever"><p>word holmes</p></a>
  • where'd the <a /> tag go??
  • mousing over the block does not get a pointer cursor, but clicking it does send you to the href target

Scenario C: adding my own a to the block of whatever


input:

<Link href="wherever">
    <a>
        <p>word holmes</p>
    </a>
</Link>

output:

<a href="wherever"><p>word holmes</p></a>
  • ok this works, but it feels hacky..
  • why does this work? Is this the "right" way to do it?
Share Improve this question edited Jun 9, 2021 at 20:44 Mike Willis asked Jun 9, 2021 at 20:37 Mike WillisMike Willis 1,52216 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

According to https://github./vercel/next.js/blob/canary/packages/next/client/link.tsx the a tag is added automatically if the child is a string. Otherwise it just returns the child. So in your case the child is a p tag, so that's all that is returned. Seems like you could just wrap that in an a tag and that should work.

发布评论

评论列表(0)

  1. 暂无评论