שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "Add usePathname utility"v10.0.023.6.2026
- "Init history"v8.2.022.6.2026
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתאם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודהעתקת ה-Markdown של המסמך ללוח
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
העתקת הקוד ללוח
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
העתקת הקוד ללוח
"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
פתיחת הטבלה בחלון כדי לראות את כל תוכן הנתונים בבירור
| 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.
העתקת הקוד ללוח
// 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
העתקת הקוד ללוח
"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;
