Link component & navigation in Next.js

From the Next.js cheat sheet · Routing & Navigation · verified Jul 2026

Link component & navigation

typescript
'use client'

import Link from 'next/link'
import { useRouter, usePathname, useSearchParams } from 'next/navigation'

export default function NavigationExample() {
  const router = useRouter()
  const pathname = usePathname()
  const searchParams = useSearchParams()

  return (
    <nav>
      {/* Basic Link */}
      <Link href="/about">About</Link>
      
      {/* Dynamic Link */}
      <Link href="/blog/my-post">Blog Post</Link>
      <Link href={`/products/${productId}`}>Product</Link>
      
      {/* With query params */}
      <Link href="/shop?category=electronics">Electronics</Link>
      <Link href={{ pathname: '/shop', query: { category: 'books' } }}>
        Books
      </Link>
      
      {/* Active link styling */}
      <Link 
        href="/dashboard"
        className={pathname === '/dashboard' ? 'active' : ''}
      >
        Dashboard
      </Link>
      
      {/* Programmatic navigation */}
      <button onClick={() => router.push('/login')}>
        Login
      </button>
      
      <button onClick={() => router.back()}>
        Go Back
      </button>
      
      <button onClick={() => router.refresh()}>
        Refresh
      </button>
      
      {/* Prefetch control */}
      <Link href="/heavy-page" prefetch={false}>
        Heavy Page (no prefetch)
      </Link>
    </nav>
  )
}
💡 Link enables client-side navigation
⚡ Prefetches links in viewport automatically
📌 Use router.push() for programmatic navigation
🔥 usePathname() gets current path

More Next.js tasks

Back to the full Next.js cheat sheet