import React from 'react' ; export interface ErrorBoundaryState { error : Error | null ; errorInfo : React . ErrorInfo | null ; } export interface FallbackProps { error : Error ; errorInfo : React . ErrorInfo ; [ k ] : any ; } export type ErrorBoundaryProps = { FallbackComponent ? : React . ComponentType < FallbackProps > ; onError ? : ( error : Error , info : React . ErrorInfo ) => void ; [ k ] : any ; } ; export class ErrorBoundary extends React . Component < React . PropsWithRef < React . PropsWithChildren < ErrorBoundaryProps > > , ErrorBoundaryState > { constructor ( props ) { super ( props ) ; this . state = { error : null , errorInfo : null , } ; } componentDidCatch ( error , errorInfo ) { const { onError } = this . props ; if ( onError ) { onError ( error , errorInfo ) ; } this . setState ( { error , errorInfo } ) ; } render ( ) { const { FallbackComponent , children , ... rest } = this . props ; const { error , errorInfo } = this . state ; if ( errorInfo ) { if ( FallbackComponent ) { return < FallbackComponent error = { error ! } errorInfo = { errorInfo } { ... rest } />; } return null ; } return children ; } }