import React, { ReactNode, ErrorInfo } from "react"; import { View, Text, StyleSheet, TouchableOpacity } from "react-native"; interface Props { children: ReactNode; } interface State { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } /** * Error Boundary Component * Catches JavaScript errors anywhere in the child component tree * and displays a fallback UI instead of crashing the app */ export class ErrorBoundary extends React.Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Log error details for debugging console.error("ErrorBoundary caught an error:", error); console.error("Error Info:", errorInfo); this.setState({ error, errorInfo, }); } resetError = () => { this.setState({ hasError: false, error: null, errorInfo: null, }); }; render() { if (this.state.hasError) { return ( ⚠️ Something Went Wrong The app encountered an unexpected error. Please try restarting. {__DEV__ && this.state.error && ( Error Details: {this.state.error.toString()} {this.state.errorInfo && ( {this.state.errorInfo.componentStack} )} )} Try Again ); } return this.props.children; } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "#f5f5f5", justifyContent: "center", alignItems: "center", padding: 20, }, content: { backgroundColor: "#ffffff", borderRadius: 12, padding: 24, alignItems: "center", shadowColor: "#000", shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 8, elevation: 5, }, title: { fontSize: 20, fontWeight: "bold", color: "#333333", marginBottom: 12, textAlign: "center", }, message: { fontSize: 16, color: "#666666", textAlign: "center", marginBottom: 20, lineHeight: 22, }, errorDetails: { backgroundColor: "#ffe6e6", borderRadius: 8, padding: 12, marginBottom: 20, maxHeight: 200, }, errorTitle: { fontWeight: "600", color: "#d32f2f", marginBottom: 8, }, errorText: { color: "#c62828", fontSize: 12, marginBottom: 8, }, stackTrace: { color: "#c62828", fontSize: 10, fontFamily: "Courier New", }, button: { backgroundColor: "#4045EF", paddingHorizontal: 32, paddingVertical: 12, borderRadius: 8, }, buttonText: { color: "#ffffff", fontSize: 16, fontWeight: "600", }, });