Next.js: Wrap <Link> around several components

Created on 18 Feb 2017  路  7Comments  路  Source: vercel/next.js

Hi,

As we can see here: http://w3c.github.io/html/single-page.html#the-a-element
It is valid to have an <a> wrapping several html tags.

But as defined here: https://github.com/zeit/next.js/blob/2.0.0-beta.31/lib/link.js#L92
We can't have multiple children inside a <Link> component.

For example, say I have the following html:

<a>
  <h3>Link text</h3>
  <img />
</a>

The only solution using next.js would be to define a jsx element before calling <Link>, but as React requires to wrap adjacent jsx element in a parent tag, the only way to store the <h3> and <img> tags in a variable would be to add an intermediate level, resulting in:

<a>
  <div>
    <h3>Link text</h3>
    <img />
  </div>
</a>

I know it doesn't seem like such a big deal to have <div> tags wrapping <Link> children, but I think it's not perfect to style these <Link> children, and add (to my mind) some useless complexity.

I don't know next.js well enough (been using it since a week only), so I can't provide an alternative solution, and maybe I got all this wrong, but if some of you have an idea about how we could get use <Link> component with an infinite number of children, it be really great 馃檪

Thanks in advance !

Most helpful comment

I tried wrapping <Link> around <a> with multiple children element like this and I didn't find any issues with it.

<Link href="#">
  <a>
    <div>Foo</div>
    <div>Bar</div>
  </a>
</Link>

FYI, <Link> is to be used as a wrapper for <a>. You can have as many children you want under <a>.

All 7 comments

I tried wrapping <Link> around <a> with multiple children element like this and I didn't find any issues with it.

<Link href="#">
  <a>
    <div>Foo</div>
    <div>Bar</div>
  </a>
</Link>

FYI, <Link> is to be used as a wrapper for <a>. You can have as many children you want under <a>.

Well, what I mean by <a> was the <Link> output, maybe I should have write the exact code I tried to run, that is the following:

<Link href="#">
  <h3> Text </h3>
  <img src="path/img.jpg" alt="" />
</Link>

EDIT: didn't see the last line of your comment. Thanks...missed that detail. Closing :)

@fabienheureux That behaviour has been deprecated. You should always use <a> in <Link />

Yep got it, makes sense, and solves my initial thought.
I should have triple-read the doc

馃槃 thanks for the feedback!

Is there any reason why we have to put an a tag inside the Link component? Why can't Link simply have an a tag as the parent tag implicitly? It just seems redundant to have to put an a tag inside a Link tag.

@rclai the reason is you can put anything which has a onClick handler. Like a button or any other custom component.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pie6k picture pie6k  路  3Comments

sospedra picture sospedra  路  3Comments

wagerfield picture wagerfield  路  3Comments

lixiaoyan picture lixiaoyan  路  3Comments

timneutkens picture timneutkens  路  3Comments