Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

route-elements.md

60 lines2.7 KB

1---
2id: route-elements
3title: Route Elements
4---
5​
6After we have defined our routes and set up data loaders for them, we can finally tell React Location what to render when those routes are matched!
7​
8There are 3 types of route elements:
9​
10- `element`: A React element to render when the route is matched.
11- `errorElement`: A React element to render when the route is matched but an error occurs.
12- `pendingElement`: A React element to render when the route is matched and enters a `pending` state.
13​
14## What exactly is an element?
15​
16We refer to route elements with the same context as React. It is the result of a rendered block of JSX, a string, number, or even `null`. Here are a few examples:
17​
18```tsx
19const routes = [
20 { path: '/', element: <h1>Home</h1> },
21 { path: '/about', element: 'about' },
22 { path: '/contact', element: <Contact /> },
23 { path: '/teams', element: <Teams customProp={'whatever'} /> },
24 { path: '/optional', element: condition ? <Optional /> : null },
26```
27​
28As long as you pass a valid JSX element, you can use it as a route element.
29​
30## The Default Elements
31​
32Routes come with a few defaults, each of which is customizable via the `Router` component.
33​
34- `element`: The `defaultElement` prop you pass to your `Router`, which defaults to `<Outlet>`.
35 - If you don't specify an element, the `defaultElement` outlet will be used instead.
36 - `<Outlet>` is the default element because it enables routes to be defined with just a path for hierarchical purposes, and simply act as a passthrough for rendering.
37- `errorElement`: The `defaultErrorElement` prop you pass to your `Router`, which defaults to `null`.
38 - In development, a default error element is provided to warn you of unhandled promises and the lack of an `errorElement` on the route
39 - In production, there is no default error element, which means unhandled route errors will bubble up to the nearest `<ErrorBoundary>`
40- `pendingElement`: The `defaultPendingElement` prop you pass to your `Router`, which defaults to `null`.
41​
42## Regular Route Elements
43​
44The regular `element` property of a route is what the router will render when a route is matched and ready to be displayed. There's not much to it!
45​
46```tsx
47const routes = [{ path: '/', element: <Home /> }]
48```
49​
50## Error Route Elements
51​
52The error `element` property of a route is what the router will render when a route loader or async element throws an error.
53​
54For more information, see the [Route Loaders - Handling Loader Errors](./route-loaders#handling-loader-errors) guide.
55​
56## Pending Route Elements
57​
58The pending `element` property of a route is what the router will render when a route loader enters the `pending` state.
59​
60Because a pending state shares some context with other sections, you can read all about it in its own [Pending States](./pending-states) guide.