A website without links is a dead end. In this lesson we cover every way to move between pages in the App Router: the Link component for normal links, the useRouter hook for moving from code, usePathname to know where you are, and useSearchParams to read the part of the address after the question mark. Our example is a small navigation bar that we run and test.
The Link component
Link looks like an anchor tag, and in fact it makes one. But it does more. When a visitor clicks it, Next.js loads the new page without reloading the whole site, so the move feels instant. When a Link scrolls into view, Next.js can also prepare the target page in the background, which is called prefetching. This is why you should use Link instead of a plain a tag for links inside your site. For a link to another website, a plain anchor is fine.
import Link from "next/link";
export default function Menu() {
return (
<nav>
<Link href="/about">About</Link>
<Link href="/blog/hello-nextjs">First post</Link>
</nav>
);
}
- href is the target address. It can be a string or an object.
- Link works in Server Components, so you can use it on any page without a special line at the top.
- Do not nest an a tag inside Link. Link already makes the anchor.
A client component for the active link
Some navigation tools need to know what is happening in the browser, so they must run there. A file that uses hooks such as usePathname or useRouter needs the line use client at the top. You will learn exactly what that line means in the Server and Client Components lesson. For now, remember: hooks from next/navigation go in a file that starts with the line use client.
"use client";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
export default function NavLinks() {
const pathname = usePathname();
const router = useRouter();
const links = [
{ href: "/", label: "Home" },
{ href: "/about", label: "About" },
{ href: "/blog", label: "Blog" },
];
return (
<nav className="flex gap-4">
{links.map((l) => (
<Link key={l.href} href={l.href} className={pathname === l.href ? "font-bold" : ""}>
{l.label}
</Link>
))}
<button onClick={() => router.push("/pricing")}>See pricing</button>
</nav>
);
}
- usePathname returns the current path, such as /about. We compare it with each link, and the active one gets a bold class.
- useRouter gives you a router object. router.push goes to a new page and adds it to history, so the Back button works. router.replace goes to a new page without adding to history, and router.back goes back. router.refresh asks for fresh data for the current page.
- Use Link when you can. Use router.push after an action, for example after a form is saved.
Reading the query string with useSearchParams
A query string is the part after a question mark, such as /?q=hello. The hook useSearchParams gives you a read-only object. You call get with a name and receive the value, or null if it is missing.
"use client";
import { useSearchParams } from "next/navigation";
export default function SearchBox() {
const params = useSearchParams();
const q = params.get("q") ?? "";
return <p>You searched for: {q || "nothing yet"}</p>;
}
In our test the address /?q=hello returned a normal page, and the search box showed the typed word. Without the q part it showed nothing yet.
Changing the query string safely
To build a new address from the current search params, create a URLSearchParams object from the old one, set the value you want and push the result. This keeps other values in place. Never join strings by hand, because special characters such as spaces and ampersands need to be encoded.
"use client";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
export default function SortButton() {
const pathname = usePathname();
const router = useRouter();
const searchParams = useSearchParams();
function sortBy(value: string) {
const next = new URLSearchParams(searchParams.toString());
next.set("sort", value);
router.push(`${pathname}?${next.toString()}`);
}
return <button onClick={() => sortBy("price")}>Sort by price</button>;
}
This button was type-checked in the lab together with the other files in the project, but it was not clicked in a browser.
Server pages can read params without hooks
A page component receives searchParams as a prop, and in Next.js 16 you await it like params. That is the way to read the query string in a Server Component. The hook is only for client components.
Remember
- Link for normal links. It is the best choice for speed and for search engines, because the target is a real anchor tag.
- useRouter for moving after an action.
- usePathname to know the current path.
- useSearchParams to read the query string in a client component.
Your task
- Build a navigation bar with Home, About and Blog links. Make the active one bold using usePathname.
- Add a button that uses router.push to open /pricing.
- Add a search box component that reads the q parameter. Wrap it in Suspense and visit /?q=test.
- Use router.back in a small Back button on the blog post page.
How to check it worked
- Clicking a link does not reload the whole page.
- The active link looks different from the others.
- The q value from the address appears on the page.