import React, { Component, ErrorInfo, ReactNode } from 'react'; import { View, Text, StyleSheet, TouchableOpacity } from 'react-native'; interface Props { children: ReactNode; fallback?: (error: Error, resetError: () => void) => ReactNode; } interface State { hasError: boolean; error: Error | null; } /** * Error Boundary component to catch and display errors gracefully */ export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo): void { console.error('Error Boundary caught error:', error, errorInfo); // Here you could log to an error reporting service like Sentry } resetError = (): void => { this.setState({ hasError: false, error: null, }); }; render(): ReactNode { if (this.state.hasError && this.state.error) { if (this.props.fallback) { return this.props.fallback(this.state.error, this.resetError); } return ( Oops! Something went wrong {this.state.error.message} Try Again ); } return this.props.children; } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#0a0a0a', justifyContent: 'center', alignItems: 'center', padding: 20, }, content: { alignItems: 'center', maxWidth: 400, }, title: { fontSize: 24, fontWeight: '600', color: '#ffffff', marginBottom: 12, textAlign: 'center', }, message: { fontSize: 16, color: '#999999', marginBottom: 24, textAlign: 'center', lineHeight: 24, }, button: { backgroundColor: '#ffffff', paddingHorizontal: 24, paddingVertical: 12, borderRadius: 8, }, buttonText: { fontSize: 16, fontWeight: '600', color: '#0a0a0a', }, });