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

CatchBoundary.tsx

120 lines3.2 KB

1import * as React from 'react'
2import type { ErrorRouteComponent } from './route'
3import type { ErrorInfo } from 'react'
4​
5export function CatchBoundary(props: {
6 getResetKey: () => number | string
7 children: React.ReactNode
8 errorComponent?: ErrorRouteComponent
9 onCatch?: (error: Error, errorInfo: ErrorInfo) => void
10}) {
11 const errorComponent = props.errorComponent ?? ErrorComponent
12​
13 return (
14 <CatchBoundaryImpl
15 getResetKey={props.getResetKey}
16 onCatch={props.onCatch}
17 children={({ error, reset }) => {
18 if (error) {
19 return React.createElement(errorComponent, {
20 error,
21 reset,
22 })
23 }
24​
25 return props.children
26 }}
27 />
28 )
30​
31class CatchBoundaryImpl extends React.Component<{
32 getResetKey: () => number | string
33 children: (props: {
34 error: Error | null
35 reset: () => void
36 }) => React.ReactNode
37 onCatch?: (error: Error, errorInfo: ErrorInfo) => void
38}> {
39 state = { error: null } as { error: Error | null; resetKey: string }
40 static getDerivedStateFromProps(props: any) {
41 return { resetKey: props.getResetKey() }
42 }
43 static getDerivedStateFromError(error: Error) {
44 return { error }
45 }
46 reset() {
47 this.setState({ error: null })
48 }
49 componentDidUpdate(
50 prevProps: Readonly<{
51 getResetKey: () => string
52 children: (props: { error: any; reset: () => void }) => any
53 onCatch?: ((error: any, info: any) => void) | undefined
54 }>,
55 prevState: any,
56 ): void {
57 if (prevState.error && prevState.resetKey !== this.state.resetKey) {
58 this.reset()
59 }
60 }
61 componentDidCatch(error: Error, errorInfo: ErrorInfo) {
62 if (this.props.onCatch) {
63 this.props.onCatch(error, errorInfo)
64 }
65 }
66 render() {
67 // If the resetKey has changed, don't render the error
68 return this.props.children({
69 error:
70 this.state.resetKey !== this.props.getResetKey()
71 ? null
72 : this.state.error,
73 reset: () => {
74 this.reset()
75 },
76 })
77 }
79​
80export function ErrorComponent({ error }: { error: any }) {
81 const [show, setShow] = React.useState(process.env.NODE_ENV !== 'production')
82​
83 return (
84 <div style={{ padding: '.5rem', maxWidth: '100%' }}>
85 <div style={{ display: 'flex', alignItems: 'center', gap: '.5rem' }}>
86 <strong style={{ fontSize: '1rem' }}>Something went wrong!</strong>
87 <button
88 style={{
89 appearance: 'none',
90 fontSize: '.6em',
91 border: '1px solid currentColor',
92 padding: '.1rem .2rem',
93 fontWeight: 'bold',
94 borderRadius: '.25rem',
95 }}
96 onClick={() => setShow((d) => !d)}
97 >
98 {show ? 'Hide Error' : 'Show Error'}
99 </button>
100 </div>
101 <div style={{ height: '.25rem' }} />
102 {show ? (
103 <div>
104 <pre
105 style={{
106 fontSize: '.7em',
107 border: '1px solid red',
108 borderRadius: '.25rem',
109 padding: '.3rem',
110 color: 'red',
111 overflow: 'auto',
112 }}
113 >
114 {error.message ? <code>{error.message}</code> : null}
115 </pre>
116 </div>
117 ) : null}
118 </div>
119 )