🎁 The Js-Craft Guide to React is now available with a 30% off discount!

Why do we need to have an < a > tag inside of a Link in NextJs?

When writing my first pages in NextJs I found it strange that all the <Link/> examples were pointing to a code like the one below:

<Link href="/to-some-page">
    <a>Go to another page</a>
</Link>

Ok, it made sense to have a Link element so that we can get the super benefit of prefetching. But why the extra nesting for an <a> element? Why don't we just write:

<Link href="/to-some-page">Go to another page</Link>

Well, I found out that this is because of how the Link component works. Basically, it attaches an onClick event to its direct child component. The actual a element becomes:

<a onclick={goToPageFunction}>Go to another page</a>

Therefore, it needs a child component to attach the onClick event to it.

The nice stuff is that we can set also other types of elements as children of a Link. Do you want a button or a span to act as a link? No problem. Just do:

<Link href="/to-some-page">
    <button>This button redirects to /to-some-page url</button>
</Link>

But what about SEO? Indeed, how can we get the SEO benefits given that the actual URL from the href of the Link it's used in the onClick function? Google uses the hrefs values as trust votes for its search mechanism.

Well, in case we want to communicate to the search bots our vote for a specific page we can use the passHref property. You will also want to use passHref if you used styled a components in NextJs. See more about this in the official docs.

Btw, in case you are interested in other out-of-the-box functionalities of the Link component in NextJs I've written another article about it. Check it out here.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.


Leave a Reply

Your email address will not be published. Required fields are marked *

📘 The Guide to React Home Screencasts Best of Newsletter Search X

📘 - 10 Javascript AI projects with React and LangChain

Hi friend! Before you go, just wanted to let you know about the 10 Javascript AI projects ideas with React and LangChain FREE ebook.

One of the best ways to learn is by doing the work. Choose from these 10 project ideas and start working on topics such as:

  • How does LangChain work
  • Connecting to OpenAI LLM
  • Create AI Agents
  • Simple and Sequential Chains
  • Adding Memory (Chat Context)
  • Prompt Templates
  • Using OutputParsers and Tools for Agents
  • Work with Documents and more...

Keep building and see the new capabilities LLM models can add to your React app! Get all projects as an ebook right to your inbox!

X