Autor:
    Data utworzenia:2026-06-22Ostatnia aktualizacja:2026-06-22

    Integracja z Preact: Dokumentacja Hooka usePathname

    Hook usePathname zwraca bieżącą ścieżkę przeglądarki (pathname) z usuniętym segmentem locale. Jest to przydatne do tworzenia nawigacji uwzględniającej wielojęzyczność — na przykład określania, który element nawigacji jest aktywny — bez konieczności ręcznego usuwania prefiksu locale.

    Importowanie usePathname w Preact

    typescript
    import { usePathname } from "preact-intlayer";

    Przegląd

    usePathname odczytuje window.location.pathname, usuwa prefiks locale za pomocą getPathWithoutLocale i ponownie renderuje komponent za każdym razem, gdy przeglądarka wywoła zdarzenie popstate (nawigacja wstecz/dalej). Podczas renderowania po stronie serwera (SSR) zwraca pusty ciąg znaków.

    Użycie

    src/components/NavItem.tsx
    import type { FunctionComponent } from "preact";import { usePathname } from "preact-intlayer";type NavItemProps = {  href: string;  label: string;};const NavItem: FunctionComponent<NavItemProps> = ({ href, label }) => {  const pathname = usePathname();  const isActive = pathname === href;  return (    <a href={href} aria-current={isActive ? "page" : undefined}>      {label}    </a>  );};export default NavItem;

    Zwracana Wartość

    Typ Opis
    string Bieżąca ścieżka bez prefiksu locale. Pusty ciąg znaków podczas renderowania po stronie serwera (SSR).

    Zachowanie

    • Usuwanie locale (Locale stripping): Usuwa początkowy segment locale (np. /pl/dashboard/dashboard).
    • Reaktywność: Aktualizuje się automatycznie przy zdarzeniach popstate (nawigacja wstecz / dalej przeglądarki).
    • Bezpieczeństwo w SSR: Zwraca "", gdy obiekt window jest niedostępny.
    • Czyszczenie (Cleanup): Nasłuchiwacz popstate jest usuwany automatycznie podczas odmontowywania komponentu.

    Przykład

    src/components/Sidebar.tsx
    import type { FunctionComponent } from "preact";import { usePathname } from "preact-intlayer";const links = [  { href: "/dashboard", label: "Pulpit" },  { href: "/settings", label: "Ustawienia" },];const Sidebar: FunctionComponent = () => {  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;

    Powiązane