생성:2026-06-22마지막 업데이트:2026-06-22
이 문서를 원하는 AI 어시스턴트에 참조하세요ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
이 페이지와 원하는 AI 어시스턴트를 사용하여 문서를 요약합니다
버전 기록
- "usePathname 유틸리티 추가"v10.0.02026. 6. 23.
- "히스토리 초기화"v8.2.02026. 6. 22.
이 페이지의 콘텐츠는 AI를 사용하여 번역되었습니다.
영어 원본 내용의 최신 버전을 보기Edit this doc
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy
Copy doc Markdown to clipboard
Preact 통합: usePathname 훅 문서
usePathname 훅은 로케일 세그먼트가 제거된 현재 브라우저의 경로명(pathname)을 반환합니다. 이 훅은 로케일 접두사를 수동으로 제거할 필요 없이 어떤 내비게이션 항목이 활성화되어 있는지 확인하는 등 로케일 인식 내비게이션을 구축할 때 유용합니다.
Preact에서 usePathname 가져오기
typescript
코드 복사
코드를 클립보드에 복사
import { usePathname } from "preact-intlayer";개요
usePathname은 window.location.pathname을 읽고 getPathWithoutLocale을 통해 로케일 접두사를 제거합니다. 브라우저가 popstate 이벤트(뒤로/앞으로 가기 내비게이션)를 발생시킬 때마다 컴포넌트를 다시 렌더링합니다. 서버 사이드 렌더링(SSR) 중에는 빈 문자열을 반환합니다.
사용법
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;반환 값
테이블의 모든 내용 표시
테이블을 모달로 열어 모든 데이터를 명확하게 확인
| 타입 | 설명 |
|---|---|
string | 로케일 접두사가 없는 현재 경로명. 서버 사이드 렌더링(SSR) 중에는 빈 문자열입니다. |
동작 방식
- 로케일 제거 (Locale stripping): URL 앞의 로케일 세그먼트를 제거합니다(예:
/ko/dashboard→/dashboard). - 반응형:
popstate이벤트(브라우저 뒤로 / 앞으로 가기 내비게이션) 시 자동으로 업데이트됩니다. - SSR 안전성:
window를 사용할 수 없을 때""를 반환합니다. - 정리 (Cleanup): 컴포넌트 마운트 해제 시
popstate리스너가 자동으로 제거됩니다.
예제
src/components/Sidebar.tsx
코드 복사
코드를 클립보드에 복사
import type { FunctionComponent } from "preact";import { usePathname } from "preact-intlayer";const links = [ { href: "/dashboard", label: "대시보드" }, { href: "/settings", label: "설정" },];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;관련 항목
useLocale— 현재 로케일 + 로케일 스위처getPathWithoutLocale— 이 훅에서 사용되는 기본 유틸리티