Skip to main content
usePathname is a Client Component hook that returns the current URL’s pathname.
app/example-client-component.tsx

Parameters

usePathname takes no parameters.

Returns

Returns a string containing the current URL’s pathname.

Good to know

  • Reading the current URL from a Server Component is not supported by design, to preserve layout state across page navigations.
  • usePathname may return null in the Pages Router when the router is not yet initialized (e.g. during fallback routes or Automatic Static Optimization).
  • When cacheComponents is enabled, usePathname may require a Suspense boundary if the route has a dynamic param (optional if you use generateStaticParams).
  • If your page is statically prerendered and your app uses rewrites, the pathname read by usePathname() on the client may differ from what the server rendered, causing hydration mismatch errors. See the example below for a mitigation pattern.

Examples

Reacting to route changes

app/example-client-component.tsx

Avoid hydration mismatch with rewrites

When a page is prerendered and later reached through a rewrite, the browser URL may differ from what usePathname() returns on the server. Design the UI so only a small, isolated part depends on the client pathname:
app/example-client-component.tsx
This renders an empty string on the server (avoiding mismatch) and populates after hydration.

Version history