StaticgetStatic lifecycle method that is called when a descendant component throws an error. Updates the state to show the fallback UI on the next render.
New state object with hasError set to true
static getDerivedStateFromError() {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
Lifecycle method that is called after an error has been thrown in a descendant component. Logs the error and component stack trace.
The error that was thrown
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(),
);
}
Renders either the children or the fallback UI based on whether an error has occurred.
The rendered component
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return this.props.fallback;
}
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.
Component
Param: props
Component props
Example
Source