Route HeroUI Pro Sidebar, Navbar, and AppLayout links through your framework's router with a global RouterProvider or the navigate prop.
HeroUI Pro navigation components accept an href on their items and route it through your framework's router, so links navigate without a full page load. Wire it up once with React Aria's RouterProvider, or per component with the navigate prop.
| Component | Link parts | navigate prop on |
|---|---|---|
| Sidebar | Sidebar.MenuItem | Sidebar.Provider |
| Navbar | Navbar.Item, Navbar.MenuItem | Navbar |
| AppLayout | The Sidebar and Navbar parts it renders | AppLayout |
Navbar.Item and Navbar.MenuItem render a real <a> element when href is set. Sidebar.MenuItem is a React Aria TreeItem, which cannot be rendered as an <a> because of HTML spec limitations, so its navigation is handled programmatically through React Aria's router.
When an item with an internal href is pressed, the first available option wins:
navigate prop on the component (Sidebar.Provider, Navbar, or AppLayout). A Navbar rendered inside AppLayout inherits the layout's navigate unless it sets its own.RouterProvider.External links and forceReload items skip client-side routing. See External Links and Force Reload.
Wrapping your app once in React Aria's RouterProvider (re-exported from @heroui/react) is the recommended setup. Every Sidebar, Navbar, and AppLayout item with an href routes through it, with no routing props on the components. With a RouterProvider in place, ⌘/Ctrl-click on a Navbar.Item still opens a new tab.
import {RouterProvider} from "@heroui/react";
import {Outlet, useHref, useNavigate} from "react-router";
export default function App() {
const navigate = useNavigate();
return (
<RouterProvider navigate={navigate} useHref={useHref}>
<main className="bg-background text-foreground min-h-dvh">
<Outlet />
</main>
</RouterProvider>
);
}useRouter only works in a client component, so put the provider in one and render it from your root layout:
// app/providers.tsx
"use client";
import {RouterProvider} from "@heroui/react";
import {useRouter} from "next/navigation";
export function Providers({children}: {children: React.ReactNode}) {
const router = useRouter();
return <RouterProvider navigate={router.push}>{children}</RouterProvider>;
}// app/layout.tsx
import {Providers} from "./providers";
export default function RootLayout({children}: {children: React.ReactNode}) {
return (
<html lang="en">
<body>
<Providers>{children}</Providers>
</body>
</html>
);
}import {RouterProvider} from "@heroui/react";
import {type NavigateOptions, Outlet, type ToOptions, useRouter} from "@tanstack/react-router";
declare module "react-aria-components" {
interface RouterConfig {
href: ToOptions["to"];
routerOptions: Omit<NavigateOptions, keyof ToOptions>;
}
}
export function RootComponent() {
const router = useRouter();
return (
<RouterProvider
navigate={(to, options) => router.navigate({...options, to})}
useHref={(to) => router.buildLocation({to}).href}
>
<Outlet />
</RouterProvider>
);
}A client-rendered app (for example, a Vite SPA) uses the setup for its router: add the React Router or TanStack Router provider above at the root of the route tree. Without a client router, leave out both RouterProvider and navigate, and items use the browser's default navigation.
With the provider in place, the components need no routing props:
import {Navbar, Sidebar} from "@heroui-pro/react";
<Navbar>
<Navbar.Header>
<Navbar.Content>
<Navbar.Item href="/features">Features</Navbar.Item>
<Navbar.Item href="/pricing">Pricing</Navbar.Item>
</Navbar.Content>
</Navbar.Header>
</Navbar>;
<Sidebar.Provider>
<Sidebar>
<Sidebar.Content>
<Sidebar.Menu>
<Sidebar.MenuItem href="/dashboard">
<Sidebar.MenuLabel>Dashboard</Sidebar.MenuLabel>
</Sidebar.MenuItem>
</Sidebar.Menu>
</Sidebar.Content>
</Sidebar>
<Sidebar.Main>{/* … */}</Sidebar.Main>
</Sidebar.Provider>;navigate PropPass your router's navigation function to a component's navigate prop when you don't have a global provider, when one component needs a different router than the rest of the app (for example, an external shell app), or when a Sidebar.MenuItem needs forceReload. An explicit navigate always wins over a global RouterProvider.
| Router | navigate value |
|---|---|
| Next.js App Router | router.push, from useRouter() in next/navigation (client component only) |
| React Router | The function returned by useNavigate() |
| TanStack Router | (href) => router.navigate({to: href}), with router from useRouter() |
"use client";
import {useRouter} from "next/navigation";
import {Navbar, Sidebar} from "@heroui-pro/react";
export function AppSidebar() {
const router = useRouter();
return (
<Sidebar.Provider navigate={router.push}>
<Sidebar>{/* Sidebar.MenuItem href="/dashboard" … */}</Sidebar>
<Sidebar.Main>{/* … */}</Sidebar.Main>
</Sidebar.Provider>
);
}
export function AppNavbar() {
const router = useRouter();
return <Navbar navigate={router.push}>{/* Navbar.Item href="/pricing" … */}</Navbar>;
}Sidebar.Provider sets up its own scoped RouterProvider from navigate, and every Sidebar.MenuItem with an href routes through it.Navbar.Item and Navbar.MenuItem with an href calls navigate. Pressing a Navbar.MenuItem also closes the mobile menu.navigate is forwarded to the internal Sidebar.Provider and inherited by the Navbar passed to the navbar prop, so you pass it once for the whole layout. A navigate set directly on that Navbar wins."use client";
import {useRouter} from "next/navigation";
import {AppLayout} from "@heroui-pro/react";
export function DashboardLayout({children}: {children: React.ReactNode}) {
const router = useRouter();
return (
<AppLayout navigate={router.push} navbar={/* … */} sidebar={/* … */}>
{children}
</AppLayout>
);
}Navbar.Item and Navbar.MenuItem are polymorphic, so you can render your framework's <Link> through the render prop to keep prefetching, scroll restoration, and other router behavior. This works the same with React Router's and TanStack Router's Link:
import Link from "next/link";
import {Navbar} from "@heroui-pro/react";
<Navbar.Item href="/features" render={(props) => <Link href="/features" {...props} />}>
Features
</Navbar.Item>;You can combine render with navigate: render owns the element and prefetching, and navigate handles programmatic navigation, such as closing the mobile menu and pushing to the href.
Sidebar.MenuItem can't use this approach because it can't render as an <a>. Use a RouterProvider or navigate instead.
href starting with http:// or https:// opens in a new tab and skips client-side routing.forceReload on an internal item to load the page with a full page reload instead of client-side navigation.<Navbar.Item href="https://github.com/heroui-inc/heroui">GitHub</Navbar.Item>
<Navbar.Item href="/legacy-page" forceReload>Legacy page</Navbar.Item>
<Sidebar.MenuItem href="/legacy-page" forceReload>
<Sidebar.MenuLabel>Legacy page</Sidebar.MenuLabel>
</Sidebar.MenuItem>On Sidebar.MenuItem, forceReload is handled by Sidebar.Provider's own router. With only a global RouterProvider (no navigate on Sidebar.Provider), forceReload is forwarded to your router as a router option and doesn't force a full page reload. Pass navigate to Sidebar.Provider (or AppLayout) when you need it.