Skip to content

Commit 8d28ee2

Browse files
authored
Create ErrorBoundary.tsx
fix: add global ErrorBoundary component and wrap _app to prevent blank-screen crash (#7872)
1 parent 84a5696 commit 8d28ee2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/components/ErrorBoundary.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use client";
2+
import { Component, ReactNode } from "react";
3+
4+
type Props = {
5+
children: ReactNode;
6+
};
7+
8+
type State = {
9+
hasError: boolean;
10+
};
11+
12+
export class ErrorBoundary extends Component<Props, State> {
13+
constructor(props: Props) {
14+
super(props);
15+
this.state = { hasError: false };
16+
}
17+
18+
static getDerivedStateFromError(_: Error) {
19+
return { hasError: true };
20+
}
21+
22+
componentDidCatch(error: Error, info: any) {
23+
console.error("Caught by ErrorBoundary:", error, info);
24+
}
25+
26+
render() {
27+
if (this.state.hasError) {
28+
return (
29+
<main style={{ padding: "2rem", textAlign: "center" }}>
30+
<h1>Something went wrong.</h1>
31+
<p>Try refreshing the page. If the problem persists, please report it.</p>
32+
</main>
33+
);
34+
}
35+
36+
return this.props.children;
37+
}
38+
}

0 commit comments

Comments
 (0)