#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
3 min read
•Question 3 of 47easy

How to use the Link component in Next.js?

Client-side navigation with Link component.

What You'll Learn

  • Using Link component
  • Prefetching behavior
  • Navigation options

Basic Usage

code.txtTSX
import Link from 'next/link';

export default function Nav() {
  return (
    <nav>
      <Link href="/">Home</Link>
      <Link href="/about">About</Link>
      <Link href="/blog/hello-world">Blog Post</Link>
    </nav>
  );
}

Dynamic Routes

code.txtTSX
// With dynamic segments
<Link href={`/blog/${post.slug}`}>Read More</Link>

// Using object
<Link
  href={{
    pathname: '/blog/[slug]',
    query: { slug: post.slug },
  }}
>
  Read More
</Link>

Prefetching

code.txtTSX
// Prefetch enabled by default in production
<Link href="/dashboard">Dashboard</Link>

// Disable prefetch
<Link href="/dashboard" prefetch={false}>
  Dashboard
</Link>

Replace History

code.txtTSX
// Replace instead of push
<Link href="/login" replace>
  Login
</Link>

Scroll Behavior

code.txtTSX
// Disable scroll to top
<Link href="/section" scroll={false}>
  Section
</Link>