Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { Component, type ErrorInfo, type ReactNode } from "react"; type Props = { children: ReactNode }; type State = { error: Error | null }; export class ErrorBoundary extends Component<Props, State> { state: State = { error: null }; static getDerivedStateFromError(error: Error): State { return { error }; } componentDidCatch(error: Error, info: ErrorInfo): void { console.error("[ErrorBoundary]", error, info.componentStack); } render(): ReactNode { if (this.state.error) { return ( <div className="error-boundary"> <h2>Something went wrong</h2> <pre>{this.state.error.message}</pre> <button type="button" className="btn btn--primary" onClick={() => this.setState({ error: null })} > Try again </button> </div> ); } return this.props.children; } } |