-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRenderError.tsx
More file actions
42 lines (33 loc) · 921 Bytes
/
RenderError.tsx
File metadata and controls
42 lines (33 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { PropsWithChildren } from 'react';
import React from 'react';
interface Props {
error?: string;
}
interface State {
hasError: boolean;
message?: string;
}
class RenderError extends React.Component<PropsWithChildren<Props>, State> {
state = { hasError: false, message: null };
static getDerivedStateFromError(error: Error) {
return { hasError: true, message: `${error.message}${error.stack}` };
}
static componentDidCatch(error: Error, info: { componentStack: string }) {
// eslint-disable-next-line no-console
console.error(error, info.componentStack);
}
render() {
const { children, error } = this.props;
const { hasError, message } = this.state;
return hasError || error ? (
<div className="error">
<pre>
<code>{message || error}</code>
</pre>
</div>
) : (
children
);
}
}
export default RenderError;