Files
wr.do/components/shared/error-boundary.tsx
2025-04-25 16:01:26 +08:00

32 lines
581 B
TypeScript

"use client";
import { Component, ReactNode } from "react";
// 错误边界组件(客户端组件)
interface ErrorBoundaryProps {
fallback: ReactNode;
children: ReactNode;
}
interface ErrorBoundaryState {
hasError: boolean;
}
export class ErrorBoundary extends Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
state: ErrorBoundaryState = { hasError: false };
static getDerivedStateFromError() {
return { hasError: true };
}
render() {
if (this.state.hasError) {
return this.props.fallback;
}
return this.props.children;
}
}