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

not-found.tsx

43 lines1.2 KB

1import * as React from 'react'
2import { isNotFound } from '@tanstack/router-core'
3import { CatchBoundary } from './CatchBoundary'
4import { useRouterState } from './useRouterState'
5import type { ErrorInfo } from 'react'
6import type { NotFoundError } from '@tanstack/router-core'
7​
8export function CatchNotFound(props: {
9 fallback?: (error: NotFoundError) => React.ReactElement
10 onCatch?: (error: Error, errorInfo: ErrorInfo) => void
11 children: React.ReactNode
12}) {
13 // TODO: Some way for the user to programmatically reset the not-found boundary?
14 const resetKey = useRouterState({
15 select: (s) => `not-found-${s.location.pathname}-${s.status}`,
16 })
17​
18 return (
19 <CatchBoundary
20 getResetKey={() => resetKey}
21 onCatch={(error, errorInfo) => {
22 if (isNotFound(error)) {
23 props.onCatch?.(error, errorInfo)
24 } else {
25 throw error
26 }
27 }}
28 errorComponent={({ error }) => {
29 if (isNotFound(error)) {
30 return props.fallback?.(error)
31 } else {
32 throw error
33 }
34 }}
35 >
36 {props.children}
37 </CatchBoundary>
38 )
40​
41export function DefaultGlobalNotFound() {
42 return <p>Not Found</p>