Reading the current URL
A Client Component can read the current pathname with usePathname from
next/navigation:
'use client';
import { usePathname } from 'next/navigation';
export function NavLink({ href, children }) {
const pathname = usePathname();
const active = pathname === href;
return <a aria-current={active ? 'page' : undefined}>{children}</a>;
}
usePathname returns the URL's path, without the query string — for
/dashboard?v=2 it returns /dashboard. It re-renders when the route changes,
which is why it powers active-link styling. Reading the URL from a Server
Component is intentionally not supported; the hook is for the client.
usePathname tells you where you are. Its companion, useLinkStatus from
next/link (Next.js 15.3+), tells you whether a link's navigation is still
pending. You call it inside a descendant of a <Link> and read pending:
'use client';
import Link from 'next/link';
import { useLinkStatus } from 'next/link';
function Hint() {
const { pending } = useLinkStatus();
return <span aria-hidden className={pending ? 'is-pending' : ''} />;
}
export function Header() {
return (
<Link href="/dashboard" prefetch={false}>
Dashboard <Hint />
</Link>
);
}
useLinkStatus is most useful when prefetching is disabled or the destination is
dynamic without a loading.tsx, so you can show a subtle inline indicator while
the transition finishes. If the route was prefetched, the pending phase is
skipped.
You are about to answer a short quiz on usePathname and useLinkStatus.