Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Add usePathname utility"v10.0.06/23/2026
- "Init history"v8.2.06/22/2026
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
Next.js Integration: usePathname Hook Documentation
The usePathname hook returns the current Next.js pathname with the locale segment stripped. It is useful for building locale-aware navigation — for example, determining which nav item is active — without having to manually remove the locale prefix.
Importing usePathname in Next.js
Copy the code to the clipboard
import { usePathname } from "next-intlayer";Overview
usePathname wraps Next.js's built-in usePathname() from next/navigation, appends any search params, and strips the locale prefix via getPathWithoutLocale. It re-renders on every client-side navigation. The hook is available only in Client Components (requires "use client").
Usage
Copy the code to the clipboard
"use client";
import type { FC } from "react";
import { usePathname } from "next-intlayer";
type NavItemProps = {
href: string;
label: string;
};
const NavItem: FC<NavItemProps> = ({ href, label }) => {
const pathname = usePathname();
const isActive = pathname === href;
return (
<a href={href} aria-current={isActive ? "page" : undefined}>
{label}
</a>
);
};
export default NavItem;Return Value
Open the table in a modal to view all data content clearly
| Type | Description |
|---|---|
string | The current pathname without the locale prefix and query params stripped of their locale parameter. |
Behavior
- Locale stripping: Removes the leading locale segment (e.g.
/en/dashboard→/dashboard). - Search param stripping: Also strips a
?locale=...query parameter when search-params routing mode is used. - Reactive: Updates on every client-side navigation via Next.js App Router.
- SSR-safe: Returns the server-side pathname during the first render, then syncs search params on the client.
Comparison with useLocale
useLocale from next-intlayer already exposes pathWithoutLocale as part of its return value. Use usePathname when you only need the path and do not need locale switching functionality.
Copy the code to the clipboard
// When you need both locale state and the path:
import { useLocale } from "next-intlayer";
const { locale, pathWithoutLocale } = useLocale();
// When you only need the path:
import { usePathname } from "next-intlayer";
const pathname = usePathname();Example
Copy the code to the clipboard
"use client";
import type { FC } from "react";
import { usePathname } from "next-intlayer";
const links = [
{ href: "/dashboard", label: "Dashboard" },
{ href: "/settings", label: "Settings" },
];
const Sidebar: FC = () => {
const pathname = usePathname();
return (
<nav>
{links.map(({ href, label }) => (
<a
key={href}
href={href}
style={{ fontWeight: pathname === href ? "bold" : "normal" }}
>
{label}
</a>
))}
</nav>
);
};
export default Sidebar;Related
useLocale— current locale + locale switcher (also exposespathWithoutLocale)getPathWithoutLocale— the underlying utility used by this hook