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.023/06/2026
- "Initialise history"v8.2.022/06/2026
The content of this page was translated using an AI.
See the last version of the original content in EnglishIf 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
React Integration: usePathname Hook Documentation
The usePathname hook from react-intlayer returns the current browser pathname with the locale segment removed. It relies on the native window.location.pathname property and reacts to browser navigation events via popstate.
Importing usePathname
Copy the code to the clipboard
import { usePathname } from "react-intlayer";Overview
Unlike framework-specific routing hooks (such as those in next-intlayer or react-router), this hook is a lightweight, framework-agnostic solution for plain React applications. It extracts the current URL and strips any matching locale prefix (e.g., /en-GB/about becomes /about).
Usage
Copy the code to the clipboard
import type { FC } from "react";
import { usePathname } from "react-intlayer";
const Navigation: FC = () => {
const pathname = usePathname();
return (
<nav>
<a
href="/home"
style={{ fontWeight: pathname === "/home" ? "bold" : "normal" }}
>
Home
</a>
<a
href="/about"
style={{ fontWeight: pathname === "/about" ? "bold" : "normal" }}
>
About
</a>
</nav>
);
};
export default Navigation;Return Value
Open the table in a modal to view all data content clearly
| Type | Description |
|---|---|
string | The browser's current pathname with the locale prefix removed (e.g. /en-GB/dashboard → /dashboard). |
Behaviour
- Locale Stripping: Under the hood, it uses the
getPathWithoutLocaleutility to automatically detect and remove the locale from the pathname based on the application's Intlayer configuration. - Reactivity: Listens to the
popstateevent. When the user navigates using the browser's back/forward buttons, or whenpushState/replaceStateare called, the hook updates the returned pathname. - SSR Fallback: On the server (where
windowis undefined), it defaults to returning/since it does not have access to the request URL by default in a pure React context.