שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "Add usePathname utility"v10.0.023.6.2026
- "Initialize history"v8.2.022.6.2026
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתאם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודהעתקת ה-Markdown של המסמך ללוח
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 and reacts to browser navigation events via popstate.
Importing usePathname
העתקת הקוד ללוח
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/about becomes /about).
Usage
העתקת הקוד ללוח
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
פתיחת הטבלה בחלון כדי לראות את כל תוכן הנתונים בבירור
| Type | Description |
|---|---|
string | The current pathname of the browser with the locale prefix removed (e.g., /fr/dashboard → /dashboard). |
Behavior
- Locale Stripping: Uses the
getPathWithoutLocaleutility under the hood 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 browser back/forward buttons or whenpushState/replaceStateis called, the hook updates the returned pathname. - SSR Fallback: On the server (where
windowis undefined), it defaults to returning/since it has no access to the request URL by default in a pure React context.
