Author:
    Creation:2026-06-22Last update:2026-06-22

    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

    typescript
    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

    src/components/NavItem.tsx
    "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.

    Behaviour

    • Locale stripping: Removes the leading locale segment (e.g. /en-GB/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.

    tsx
    // 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

    src/components/Sidebar.tsx
    "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;