File tree Expand file tree Collapse file tree 2 files changed +239
-187
lines changed Expand file tree Collapse file tree 2 files changed +239
-187
lines changed Original file line number Diff line number Diff line change
1
+ import * as React from "react" ;
2
+
3
+ interface ErrorBoundaryProps {
4
+ children : React . ReactNode ;
5
+ renderError : ( error : Error , errorInfo : React . ErrorInfo ) => React . ReactNode ;
6
+ }
7
+
8
+ interface ErrorBoundaryState {
9
+ hasError : boolean ;
10
+ error : Error | null ;
11
+ errorInfo : React . ErrorInfo | null ;
12
+ }
13
+
14
+ class ErrorBoundary extends React . Component <
15
+ ErrorBoundaryProps ,
16
+ ErrorBoundaryState
17
+ > {
18
+ state : ErrorBoundaryState = {
19
+ hasError : false ,
20
+ error : null ,
21
+ errorInfo : null
22
+ } ;
23
+
24
+ constructor ( props : ErrorBoundaryProps ) {
25
+ super ( props ) ;
26
+ }
27
+
28
+ componentDidCatch ( error : Error , errorInfo : React . ErrorInfo ) {
29
+ this . setState ( {
30
+ hasError : true ,
31
+ error : error ,
32
+ errorInfo : errorInfo
33
+ } ) ;
34
+ }
35
+
36
+ render ( ) {
37
+ if ( this . state . hasError ) {
38
+ return this . props . renderError ( this . state . error , this . state . errorInfo ) ;
39
+ }
40
+
41
+ return this . props . children ;
42
+ }
43
+ }
44
+
45
+ export default ErrorBoundary ;
You can’t perform that action at this time.
0 commit comments