Docs
useLocation
A hook that allows you to access and manage the current location in your application
Loading...
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-location
Usage
"use client"
import { useLocation } from "@/hooks/use-location"
export function Component() {
const location = useLocation()
const handleNavigate = () => {
// Navigation in an event handler (safe)
history.pushState({ page: "new" }, "", "/new-path")
}
React.useEffect(() => {
// Navigation after render (safe)
if (location.pathname === "/") {
history.pushState({ page: "home" }, "", "/home")
}
}, [location.pathname])
return (
<div>
<h1>Current Location</h1>
<pre>{JSON.stringify(location, null, 2)}</pre>
<button onClick={handleNavigate}>Go to New Path</button>
</div>
)
}
API Reference
Return Values
Property | Initial Load (trigger: "load" ) | After pushState (/new-path ) | After Back (popstate ) | Server-Side Rendering (No Client) |
---|---|---|---|---|
trigger | "load" | "pushstate" | "popstate" | "load" |
state | null | { page: "new" } | null | null |
length | 1 | 2 | 2 | 1 |
hash | "" | "" | "" | "" |
host | "usekit.kiron.dev" | "usekit.kiron.dev" | "usekit.kiron.dev" | "" |
hostname | "usekit.kiron.dev" | "usekit.kiron.dev" | "usekit.kiron.dev" | "" |
href | "https://usekit.kiron.dev/" | "https://usekit.kiron.dev/new-path" | "https://usekit.kiron.dev/" | "" |
origin | "https://usekit.kiron.dev" | "https://usekit.kiron.dev" | "https://usekit.kiron.dev" | "" |
pathname | "/" | "/new-path" | "/" | "" |
port | "" | "" | "" | "" |
protocol | "https:" | "https:" | "https:" | "" |
search | "" | "" | "" | "" |