import * as React from 'react' import type { ErrorRouteComponent } from './route' import type { ErrorInfo } from 'react' export function CatchBoundary(props: { getResetKey: () => number | string children: React.ReactNode errorComponent?: ErrorRouteComponent onCatch?: (error: Error, errorInfo: ErrorInfo) => void }) { const errorComponent = props.errorComponent ?? ErrorComponent return ( { if (error) { return React.createElement(errorComponent, { error, reset, }) } return props.children }} /> ) } class CatchBoundaryImpl extends React.Component<{ getResetKey: () => number | string children: (props: { error: Error | null reset: () => void }) => React.ReactNode onCatch?: (error: Error, errorInfo: ErrorInfo) => void }> { state = { error: null } as { error: Error | null; resetKey: string } static getDerivedStateFromProps(props: any) { return { resetKey: props.getResetKey() } } static getDerivedStateFromError(error: Error) { return { error } } reset() { this.setState({ error: null }) } componentDidUpdate( prevProps: Readonly<{ getResetKey: () => string children: (props: { error: any; reset: () => void }) => any onCatch?: ((error: any, info: any) => void) | undefined }>, prevState: any, ): void { if (prevState.error && prevState.resetKey !== this.state.resetKey) { this.reset() } } componentDidCatch(error: Error, errorInfo: ErrorInfo) { if (this.props.onCatch) { this.props.onCatch(error, errorInfo) } } render() { // If the resetKey has changed, don't render the error return this.props.children({ error: this.state.resetKey !== this.props.getResetKey() ? null : this.state.error, reset: () => { this.reset() }, }) } } export function ErrorComponent({ error }: { error: any }) { const [show, setShow] = React.useState(process.env.NODE_ENV !== 'production') return (
Something went wrong!
{show ? (
            {error.message ? {error.message} : null}
          
) : null}
) }