Returns the current pathname without query parameters or hash.
import { usePathname } from 'one'
export default function CurrentPath() {
const pathname = usePathname()
// URL: /blog/hello-world?ref=twitter#comments
// Returns: "/blog/hello-world"
return <Text>Current path: {pathname}</Text>
}
import { usePathname, Link } from 'one'
function NavLink({ href, children }) {
const pathname = usePathname()
const isActive = pathname === href
return (
<Link
href={href}
style={{ fontWeight: isActive ? 'bold' : 'normal' }}
>
{children}
</Link>
)
}
function Layout({ children }) {
const pathname = usePathname()
const showSidebar = !pathname.startsWith('/auth')
return (
<div>
{showSidebar && <Sidebar />}
{children}
</div>
)
}
function PageTracker() {
const pathname = usePathname()
useEffect(() => {
analytics.trackPageView(pathname)
}, [pathname])
return null
}
The pathname does not include:
?foo=bar)#section)For URL parameters, use useParams or useActiveParams.
Edit this page on GitHub.