Skip to content

Commit f9190a2

Browse files
Use ErrorBoundary to catch both render-time and mount-time errors
1 parent 60b7472 commit f9190a2

File tree

2 files changed

+239
-187
lines changed

2 files changed

+239
-187
lines changed

client/Components/ErrorBoundary.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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;

0 commit comments

Comments
 (0)