Linking and Navigating
Bini.js provides built-in navigation components and hooks for fast, client-side transitions between routes without full page reloads. This page covers how to use Link, NavLink, and the navigation hooks.
Link component
The <Link> component is the primary way to navigate between routes. It extends the HTML <a> tag to provide client-side navigation, and it is auto-imported in all pages and layouts.
export default function Home() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/blog">Blog</Link>
</nav>
)
}Link props
| Prop | Type | Description |
|---|---|---|
| to | string | The destination route path |
| replace | boolean | Replace the current entry in history instead of adding |
| state | any | State to persist to the location |
| className | string | CSS class for styling |
| children | ReactNode | The content inside the link |
useParams hook
The useParams hook returns an object of key/value pairs of the dynamic route parameters from the current URL. Wrap a folder name in square brackets, like [slug], to create a dynamic segment.
export default function BlogPost() {
const { slug } = useParams<{ slug: string }>()
return (
<div>
<h1>Blog Post: {slug}</h1>
</div>
)
}useLocation hook
The useLocation hook returns the current location object, including pathname, search, hash, and any state passed during navigation. This is useful for things like breadcrumbs.
export default function Breadcrumbs() {
const location = useLocation()
const segments = location.pathname.split('/').filter(Boolean)
return (
<nav>
<Link to="/">Home</Link>
{segments.map((name, index) => {
const routeTo = `/${segments.slice(0, index + 1).join('/')}`
return (
<Link key={routeTo} to={routeTo}>
{name}
</Link>
)
})}
</nav>
)
}Location properties
| Property | Type | Description |
|---|---|---|
| pathname | string | The path of the current URL |
| search | string | The query string, including the leading ? |
| hash | string | The URL hash, including the leading # |
| state | any | State passed via Link or navigate |
| key | string | A unique key for this location entry |
useSearchParams hook
The useSearchParams hook reads and updates the query string. It works like useState: you get the current params and a function to change them.
export default function ShopPage() {
const [searchParams, setSearchParams] = useSearchParams()
const category = searchParams.get('category') || 'all'
return (
<div>
<p>Category: {category}</p>
<button onClick={() => setSearchParams({ category: 'shoes' })}>
Show shoes
</button>
</div>
)
}Best practices
- Use
<Link>for standard navigation links. - Use
<NavLink>for navigation menus that need active-state styling. - Use
useNavigatefor programmatic navigation, such as redirects after a form or login. - Use
useParamsto access dynamic route parameters. - Use
useSearchParamsfor managing query strings and filters.