forked from reactjs/react.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorBoundary.tsx
More file actions
38 lines (31 loc) · 818 Bytes
/
ErrorBoundary.tsx
File metadata and controls
38 lines (31 loc) · 818 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
"use client";
import { Component, ReactNode } from "react";
type Props = {
children: ReactNode;
};
type State = {
hasError: boolean;
};
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(_: Error) {
return { hasError: true };
}
componentDidCatch(error: Error, info: any) {
console.error("Caught by ErrorBoundary:", error, info);
}
render() {
if (this.state.hasError) {
return (
<main style={{ padding: "2rem", textAlign: "center" }}>
<h1>Something went wrong.</h1>
<p>Try refreshing the page. If the problem persists, please report it.</p>
</main>
);
}
return this.props.children;
}
}