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

renderRouteNotFound.tsx

34 lines1.3 KB

1import warning from 'tiny-warning'
2import { DefaultGlobalNotFound } from './not-found'
3import type { AnyRoute, AnyRouter } from '@tanstack/router-core'
4​
5/**
6 * Renders a not found component for a route when no matching route is found.
7 *
8 * @param router - The router instance containing the route configuration
9 * @param route - The route that triggered the not found state
10 * @param data - Additional data to pass to the not found component
11 * @returns The rendered not found component or a default fallback component
12 */
13export function renderRouteNotFound(
14 router: AnyRouter,
15 route: AnyRoute,
16 data: any,
17) {
18 if (!route.options.notFoundComponent) {
19 if (router.options.defaultNotFoundComponent) {
20 return <router.options.defaultNotFoundComponent {...data} />
21 }
22​
23 if (process.env.NODE_ENV === 'development') {
24 warning(
25 route.options.notFoundComponent,
26 `A notFoundError was encountered on the route with ID "${route.id}", but a notFoundComponent option was not configured, nor was a router level defaultNotFoundComponent configured. Consider configuring at least one of these to avoid TanStack Router's overly generic defaultNotFoundComponent (<p>Not Found</p>)`,
27 )
28 }
29​
30 return <DefaultGlobalNotFound />
31 }
32​
33 return <route.options.notFoundComponent {...data} />