StaticgetStatic lifecycle method that is called when a descendant component throws an error. Updates the state to show the fallback UI and retain the error on the next render.
The error that was thrown
New state object with hasError set to true and the error captured
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
// Update state so the next render will show the fallback UI.
return { hasError: true, error };
}
Lifecycle method that is called after an error has been thrown in a descendant component. Logs the error and component stack trace, and retains the component stack for reporting.
The error that was thrown
React-supplied error info, including the component stack
componentDidCatch(error: Error, info: { componentStack: string }) {
console.error(
error,
// Example "componentStack":
// in ComponentThatThrows (created by App)
// in ErrorBoundary (created by App)
// in div (created by App)
// in App
info.componentStack,
// Warning: `captureOwnerStack` is not available in production.
captureOwnerStack(),
);
this.setState({ componentStack: info.componentStack });
// Report the crash to GA4 (non-PII, best-effort).
void trackRenderError(error);
}
Opens the bug-report dialog prefilled with the caught error and component stack.
Renders either the children or the fallback UI (plus a report button) based on whether an error has occurred.
The rendered component
render() {
if (this.state.hasError) {
// Rendered above the app's ThemeProvider, so use a plain, self-contained
// button rather than a MUI component that would lack a theme here.
return (
<>
{this.props.fallback}
<button
type="button"
data-testid="error-boundary-report"
onClick={this.handleReport}
style={{
display: 'block',
margin: '8px auto',
padding: '6px 14px',
font: 'inherit',
cursor: 'pointer',
borderRadius: 6,
border: '1px solid currentColor',
background: 'transparent',
color: 'inherit',
}}
>
{i18n('error_boundary_report')}
</button>
</>
);
}
return this.props.children;
}
ErrorBoundary component that catches JavaScript errors anywhere in their child component tree, logs those errors, and displays a fallback UI instead of the component tree that crashed. The fallback includes a "Report Error" button that opens the bug-report dialog prefilled with the caught error and its component stack.
Component
Param: props
Component props
Example
Source