Client URL hooks
Use usePathname and useSearchParams in a Client Component when UI needs the current browser route or query during navigation. The pathname excludes the query string. useSearchParams gives a read-only query interface, unlike the plain object received by a server Page.
Scope: Next.js 16.x candidate, App Router only, Cache Components disabled, TypeScript unless otherwise labeled. Examples are static source walkthroughs; exact framework patch and build are not verified.
app/catalog/RouteSummary.tsx
'use client';import { usePathname, useSearchParams } from 'next/navigation';export default function RouteSummary() { const pathname = usePathname(); const query = useSearchParams(); return <p>Path: {pathname}; sort: {query.get('sort') ?? 'title'}</p>;}On /catalog?sort=price the component is expected to display Path: /catalog; sort: price. get returns the first value, or null when the key is absent; getAll returns all values. This example assumes an App Router-only project without rewrites.
Keep the interactive boundary small
For a static App Router page using useSearchParams in a Client Component, the documented production build requires a Suspense boundary around the affected subtree; development may not reveal the missing boundary. A deliberately request-time page has a different rendering contract. Use the server Page prop for data-dependent server filtering instead of moving all data into client code. To update a query, copy its values into a new URLSearchParams and navigate to a known path; the hook result itself is read-only. Rewrites or hybrid router setups require additional compatibility review.
app/help/page.tsx (static demonstration)
import { Suspense } from 'react';import RouteSummary from '../catalog/RouteSummary';export default function Help() { return <main><h1>Help</h1> <Suspense fallback={<p>Reading URL…</p>}><RouteSummary /></Suspense> </main>;}The static help shell can display a fallback while the client URL-reading subtree becomes available. The final summary should match the visited path and query; verify in a production build, not only development.
Common mistake
Do not import App Router hooks into a Server Component or mutate the result of useSearchParams. Do not assume an untrusted query string is safe because it was read by a hook.
Try it yourself
Add a Link to /help?sort=price and compare the summary on direct load and soft navigation. Explain why the server page approach is preferable for actual database filtering.
After this lesson, you should be able to: Choose usePathname or useSearchParams; Identify Suspense requirements.