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 astring containing the current URL’s pathname.
| URL | Returned value |
|---|---|
/ | '/' |
/dashboard | '/dashboard' |
/dashboard?v=2 | '/dashboard' |
/blog/hello-world | '/blog/hello-world' |
Good to know
- Reading the current URL from a Server Component is not supported by design, to preserve layout state across page navigations.
usePathnamemay returnnullin the Pages Router when the router is not yet initialized (e.g. during fallback routes or Automatic Static Optimization).- When
cacheComponentsis enabled,usePathnamemay require aSuspenseboundary if the route has a dynamic param (optional if you usegenerateStaticParams). - 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 whatusePathname() returns on the server. Design the UI so only a small, isolated part depends on the client pathname:
app/example-client-component.tsx
Version history
| Version | Changes |
|---|---|
v13.0.0 | usePathname introduced. |
