Next.jsEasy
What is the Link component and how does it work?
<Link> from next/link renders a real <a> tag but intercepts clicks to perform client-side navigation, and it prefetches the destination when it enters the viewport. Shared layouts stay mounted, so only the changed part of the page re-renders.
What happens on click
- Next.js prevents the full page load.
- It uses the prefetched RSC payload if available, otherwise fetches it.
- It renders the new segments while keeping parent layouts and their state.
- It updates the URL and manages scroll and focus.
Modifier clicks (Cmd/Ctrl+click) and external URLs behave like normal links.
Key props
hrefstring or URL object{ pathname, query }.replacereplace the history entry instead of pushing.scrollsetfalseto keep the scroll position.prefetch"auto"/null(default),true, orfalse.onNavigateruns only for client-side navigations and canpreventDefault(), useful to block leaving a dirty form.transitionTypes(16.2+) passes types to React View Transitions for different animations.- Any
<a>attribute (className,target,aria-*) passes through.
Related tools
useLinkStatus()shows a pending state for the clicked link.usePathname()helps mark the active link.typedRoutes: truetype-checkshrefvalues.
Interview point
Use <Link> instead of <a> for internal routes; an <a> triggers a full reload and loses client state. Use useRouter only when navigation depends on logic.
Code example
tsx
1// app/components/nav.tsx2"use client";34import Link from "next/link";5import { usePathname } from "next/navigation";67const links = [8 { href: "/", label: "Home" },9 { href: "/blog", label: "Blog" },10 { href: "/pricing", label: "Pricing" },11] as const;1213export function Nav() {14 const pathname = usePathname();1516 return (17 <nav className="flex gap-4">18 {links.map((l) => (19 <Link20 key={l.href}21 href={l.href}22 aria-current={pathname === l.href ? "page" : undefined}23 className={pathname === l.href ? "font-bold" : ""}24 >25 {l.label}26 </Link>27 ))}28 <Link href={{ pathname: "/search", query: { q: "nextjs" } }} prefetch={false}>29 Search30 </Link>31 <Link href="https://nextjs.org" target="_blank" rel="noreferrer">Docs</Link>32 </nav>33 );34}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.