usePathname
usePathname
is a Client Component hook that lets you read the current URL's pathname.
'use client';
import { usePathname } from 'next/navigation';
export default function ExampleClientComponent() {
const pathname = usePathname();
return <>Current pathname: {pathname}</>;
}
usePathname
is a Client Component hook and is not supported in Server Components.const pathname = usePathname();
usePathname
does not take any parameters.
usePathname
returns a string of the current URL's pathname. For example:
URL | Returned value |
---|---|
/ | '/' |
/dashboard | '/dashboard' |
/dashboard?v=2 | '/dashboard' |
/blog/hello-world | '/blog/hello-world' |
'use client';
import { usePathname, useSearchParams } from 'next/navigation';
function ExampleClientComponent() {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
// Do something here...
}, [pathname, searchParams]);
}