Penulis:
    Dibuat:2026-06-22Terakhir diperbarui:2026-06-22

    Integrasi Solid: Dokumentasi Hook usePathname

    Hook usePathname mengembalikan pathname browser saat ini dengan segmen locale yang telah dihapus, sebagai Solid Accessor<string>. Ini berguna untuk membangun navigasi yang sadar-locale — misalnya, menentukan item navigasi mana yang aktif — tanpa harus menghapus awalan locale secara manual.

    Mengimpor usePathname di Solid

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

    Ringkasan

    usePathname membuat sinyal reaktif yang diinisialisasi dari window.location.pathname, menghapus awalan locale melalui getPathWithoutLocale, dan memperbarui sinyal setiap kali browser memicu event popstate (navigasi kembali/maju). Event listener dibersihkan secara otomatis melalui onCleanup.

    Penggunaan

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

    Nilai Kembalian

    Tipe Deskripsi
    Accessor<string> Accessor Solid (getter reaktif) yang mengembalikan pathname saat ini tanpa awalan locale.

    Perilaku

    • Penghapusan Locale: Menghapus segmen locale di awal (misal: /id/dashboard/dashboard).
    • Reaktif: Memperbarui secara otomatis pada event popstate (navigasi kembali / maju browser).
    • Aman untuk SSR: Mengembalikan "" saat window tidak tersedia.
    • Pembersihan: Listener popstate dihapus secara otomatis melalui onCleanup milik Solid.

    Contoh

    src/components/Sidebar.tsx
    import type { Component } from "solid-js";import { For } from "solid-js";import { usePathname } from "solid-intlayer";const links = [  { href: "/dashboard", label: "Dasbor" },  { href: "/settings", label: "Pengaturan" },];const Sidebar: Component = () => {  const pathname = usePathname();  return (    <nav>      <For each={links}>        {({ href, label }) => (          <a            href={href}            style={{ "font-weight": pathname() === href ? "bold" : "normal" }}          >            {label}          </a>        )}      </For>    </nav>  );};export default Sidebar;

    Terkait