Open History to see the last commit
1import * as React from 'react'
2import { DefaultGlobalNotFound } from './not-found'
3import { wrapInNonRouteComponentContext } from './nonRouteComponentContext'
4import type { AnyRoute, AnyRouter } from '@tanstack/router-core'
5β
6/**
7 * Renders a not found component for a route when no matching route is found.
8 *
9 * @param router - The router instance containing the route configuration
10 * @param route - The route that triggered the not found state
11 * @param data - Additional data to pass to the not found component
12 * @returns The rendered not found component or a default fallback component
13 */
14export function renderRouteNotFound(
15 router: AnyRouter,
16 route: AnyRoute,
17 data: any,
18) {
19 if (!route.options.notFoundComponent) {
20 if (router.options.defaultNotFoundComponent) {
21 const notFoundElement = (
22 <router.options.defaultNotFoundComponent {...data} />
23 )
24 return process.env.NODE_ENV !== 'production'
25 ? wrapInNonRouteComponentContext(notFoundElement, 'notFoundComponent')
26 : notFoundElement
27 }
28β
29 if (process.env.NODE_ENV !== 'production') {
30 if (!route.options.notFoundComponent) {
31 console.warn(
32 `Warning: 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>)`,
33 )
34 }
35 }
36β
37 return <DefaultGlobalNotFound />
38 }
39β
40 const notFoundElement = <route.options.notFoundComponent {...data} />
41 return process.env.NODE_ENV !== 'production'
42 ? wrapInNonRouteComponentContext(notFoundElement, 'notFoundComponent')
43 : notFoundElement
44}