import React, { Component, ErrorInfo, ReactNode } from 'react'; import { View, Text, StyleSheet, TouchableOpacity } from 'react-native'; import { Colors } from '../../constants/Colors'; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error: Error | null; } 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) { console.error('ErrorBoundary caught an error:', error, errorInfo); // In production, send to error tracking service (Sentry) } handleReset = () => { this.setState({ hasError: false, error: null }); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return ( Something went wrong {this.state.error?.message || 'An unexpected error occurred'} Try Again ); } return this.props.children; } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20, backgroundColor: Colors.background, }, title: { fontSize: 24, fontWeight: 'bold', color: Colors.text, marginBottom: 12, }, message: { fontSize: 16, color: Colors.textSecondary, textAlign: 'center', marginBottom: 24, }, button: { backgroundColor: Colors.accent, paddingHorizontal: 24, paddingVertical: 12, borderRadius: 8, }, buttonText: { color: Colors.text, fontSize: 16, fontWeight: '600', }, });