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

Exploring the Link component in NextJs: objects as hrefs, disabling prefetching and page scroll, opening in new tab

Link is maybe the most used building component from NextJs. This article aims to explore some of its most known (and less unknown) features.

So, let's start by seeing it in action. First, we will need to import it from next/link and use its href attribute to allow the user to navigate to different pages.

import Link from 'next/link'
export default ()=> {
    return (<nav>
        <Link href="/home"><a>Index</a></Link>
        <Link href="/contact"><a>Contact</a></Link>
    </nav>)
}

NextJs using objects as hrefs for a Link

One lesser-known fact about the href is that you can also send it a javascript object as a parameter. This provides a nicer separation between the actual path and query parameters:

const obj1 = {
    pathname: '/blog/[slug]',
    query: { slug: 'my-post' }
}
const obj2 = {
    pathname: '/about',
    query: { name: 'test' }
}

// will link to /blog/my-post
<Link href={obj1}>Link1</Link>

// will link to /about?name=test
<Link href={obj2}>Link2</Link>

NextJs remove the prefeching for a Link

One huge performance advantage of using the Link component is its prefeching capability for the linked local pages. If you want to reduce the data usage and disable prefetching you can say:

<Link href={url} prefetch={false}>
    <a>Open link</a>
</Link>

However keep in mind that prefetching will still take place on the hover event for the Link elements, even if prefetch is set to false.

Links in NextJs using replace and scroll

Two other properties that allow extra customization are the replace and scroll attributes. The first one will replace the current url in the browser history (the back button will not work). While the second one will prevent scrolling to the target page top, or hash id if provided:

<Link href={url} replace scroll={false}>
    <a>Goto link</a>
</Link>

NextJs open a Link in a new tab

And a final tip. If you want to open a <Link /> in a new tab you can do:

<Link href={url}>
  <a target="_blank" rel="noopener">Open this in a new tab</a>
</Link>

Be sure to take a look at the official documentation and if you are wondering why we need to place an <a> inside of an <Link> I've written another article about this here.

And speaking of opening links in new tabs don't forget to include the rel="noopener" in order to avoid any security vulnerabilities.

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