Error recovery strategies:
1. Error boundaries:
1class ErrorBoundary extends React.Component {2 state = { hasError: false };34 static getDerivedStateFromError() {5 return { hasError: true };6 }78 render() {9 if (this.state.hasError) {10 return (11 <View>12 <Text>Something went wrong</Text>13 <Button title="Retry" onPress={() => this.setState({ hasError: false })} />14 </View>15 );16 }17 return this.props.children;18 }19}
2. API retry logic:
1async function fetchWithRetry(url, retries = 3) {2 for (let i = 0; i < retries; i++) {3 try {4 return await fetch(url);5 } catch (e) {6 if (i === retries - 1) throw e;7 await new Promise(r => setTimeout(r, 1000 * (i + 1)));8 }9 }10}
Best practices: