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 | 3x 3x 2x 1x 5x 2x 3x | import React from 'react';
/**
* Catches unhandled React rendering errors and displays a fallback UI
* instead of a blank page. Wrap any subtree that may throw during render.
*/
export default class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, info) {
console.error('ErrorBoundary caught an error:', error, info);
}
render() {
if (this.state.hasError) {
return (
<div style={{ padding: 20, color: '#ffcccb', background: '#0a0a0a' }}>
<h2>Something went wrong while rendering the app.</h2>
<pre style={{ whiteSpace: 'pre-wrap' }}>{String(this.state.error)}</pre>
</div>
);
}
return this.props.children;
}
}
|