UNPKG

3.16 kB Markdown View Raw
1---
2title: Using handle
3---
4
5# Using `handle`
6
7[MODES: framework]
8
9You can build dynamic UI elements like breadcrumbs based on your route hierarchy using the [`useMatches`][use-matches] hook and [`handle`][handle] route exports.
10
11## Understanding the Basics
12
13React Router provides access to all route matches and their data throughout your component tree. This allows routes to contribute metadata through the `handle` export that can be rendered by ancestor components.
14
15The `useMatches` hook combined with `handle` exports enables routes to contribute to rendering processes higher up the component tree than their actual render point. While we'll use breadcrumbs as an example, this pattern works for any scenario where you need routes to provide additional information to their ancestors.
16
17## Defining Route `handle`s
18
19We'll use a route structure like the following:
20
21```ts filename=app/routes.ts
22import { route } from "@react-router/dev/routes";
23
24export default [
25 route("parent", "./routes/parent.tsx", [
26 route("child", "./routes/child.tsx"),
27 ]),
28] satisfies RouteConfig;
29```
30
31Add a `breadcrumb` property to the "parent" route's `handle` export. You can name this property whatever makes sense for your use case.
32
33```tsx filename=app/routes/parent.tsx
34import { Link } from "react-router";
35
36export const handle = {
37 breadcrumb: () => <Link to="/parent">Some Route</Link>,
38};
39```
40
41You can define breadcrumbs for child routes as well:
42
43```tsx filename=app/routes/child.tsx
44import { Link } from "react-router";
45
46export const handle = {
47 breadcrumb: () => (
48 <Link to="/parent/child">Child Route</Link>
49 ),
50};
51```
52
53## Using Route `handle`s
54
55Use the `useMatches` hook in your root layout or any ancestor component to collect and render the components defined in the `handle` export(s):
56
57```tsx filename=app/root.tsx lines=[7,11,22-31]
58import {
59 Links,
60 Meta,
61 Outlet,
62 Scripts,
63 ScrollRestoration,
64 useMatches,
65} from "react-router";
66
67export function Layout({ children }) {
68 const matches = useMatches();
69
70 return (
71 <html lang="en">
72 <head>
73 <Meta />
74 <Links />
75 </head>
76 <body>
77 <header>
78 <ol>
79 {matches
80 .filter(
81 (match) =>
82 match.handle && match.handle.breadcrumb,
83 )
84 .map((match, index) => (
85 <li key={index}>
86 {match.handle.breadcrumb(match)}
87 </li>
88 ))}
89 </ol>
90 </header>
91 {children}
92 <ScrollRestoration />
93 <Scripts />
94 </body>
95 </html>
96 );
97}
98
99export default function App() {
100 return <Outlet />;
101}
102```
103
104The `match` object is passed to each breadcrumb function, giving you access to `match.data` (from loaders) and other route information to create dynamic breadcrumbs based on your route's data.
105
106This pattern provides a clean way for routes to contribute metadata that can be consumed and rendered by ancestor components.
107
108## Additional Resources
109
110- [`useMatches`][use-matches]
111- [`handle`][handle]
112
113[use-matches]: ../api/hooks/useMatches
114[handle]: ../start/framework/route-module#handle