useRoutes
Hook that returns the list of your app’s pages, each with a path and a name, for building menus.
Usage
app/layout.tsx
import { Outlet, Link } from "react-router-dom";
import { Menu } from "antd";
import { useRoutes } from "app/hooks";
export default function RootLayout() {
const routes = useRoutes();
return (
<>
<Menu mode="horizontal" items={routes.map((r) => ({ key: r.path, label: <Link to={r.path}>{r.name}</Link> }))} />
<Outlet />
</>
);
}Import it from app/hooks, not from react-router-dom. The useRoutes hook of react-router-dom is a different hook.
Signature
app/hooks.ts
function useRoutes(): Array<{ path: string; name: string }>;Parameters
None.
Returns
An array of { path, name }, one item per registered page.tsx. The array is ready on the first render; there is nothing to wait for.
Examples
Only the pages under /reports:
app/reports/layout.tsx
const reports = useRoutes().filter((r) => r.path.startsWith("/reports/"));Next steps
- Pages and layouts: which file becomes which route.
- App frame and page header: a menu built from the route list.
Last updated