Created
January 31, 2025 09:12
-
-
Save Maxiviper117/4f106c0982396ad90c2c71c398805b49 to your computer and use it in GitHub Desktop.
Flexible and reusable React error boundary with custom fallback, error logging, and reset functionality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, type ReactNode } from "react"; | |
interface ErrorBoundaryProps { | |
children: ReactNode; | |
fallback?: ReactNode; | |
onError?: (error: Error, errorInfo: React.ErrorInfo) => void; | |
resetButtonText?: string; | |
showResetButton?: boolean; | |
} | |
interface ErrorBoundaryState { | |
hasError: boolean; | |
error?: Error; | |
} | |
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | |
constructor(props: ErrorBoundaryProps) { | |
super(props); | |
this.state = { hasError: false, error: undefined }; | |
} | |
static getDerivedStateFromError(error: Error): ErrorBoundaryState { | |
return { hasError: true, error }; | |
} | |
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { | |
console.error("ErrorBoundary caught an error:", error, errorInfo); | |
if (this.props.onError) { | |
this.props.onError(error, errorInfo); | |
} | |
} | |
resetErrorBoundary = () => { | |
this.setState({ hasError: false, error: undefined }); | |
}; | |
render() { | |
const { hasError, error } = this.state; | |
const { | |
fallback, | |
children, | |
resetButtonText = "Try Again", | |
showResetButton = true, | |
} = this.props; | |
if (hasError) { | |
return ( | |
<div> | |
{fallback ? ( | |
fallback | |
) : ( | |
<> | |
<h2>Something went wrong</h2> | |
{error && ( | |
<> | |
<p>Error type: {error.name}</p> | |
<p>Message: {error.message}</p> | |
</> | |
)} | |
</> | |
)} | |
{showResetButton && ( | |
<button onClick={this.resetErrorBoundary}> | |
{resetButtonText} | |
</button> | |
)} | |
</div> | |
); | |
} | |
return children; | |
} | |
} | |
export default ErrorBoundary; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π React Error Boundary with Enhanced Customization
This
ErrorBoundary
component provides a flexible way to handle runtime errors in React applications.It supports a custom fallback UI, error logging, custom reset button text, and an option to hide the reset button.
π οΈ Props Explanation
children
ReactNode
fallback
ReactNode
undefined
onError
(error, errorInfo) => void
undefined
resetButtonText
string
"Try Again"
showResetButton
boolean
true
π Usage Examples
1οΈβ£ Basic Usage (Default Fallback UI)
2οΈβ£ Custom Fallback UI
3οΈβ£ Logging Errors (Sending to Monitoring Services)
4οΈβ£ Custom Reset Button Text
5οΈβ£ Hide Reset Button
6οΈβ£ Combine Everything
β Why Use This?
β Prevents full page crashes by isolating errors.
β Provides a better user experience with a retry option.
β Customizable fallback UI and logging for better debugging.
β Fully configurable error boundary for different use cases.
π Copy and use this in your React projects! π