Kantongku: hardening - ErrorBoundary + catch semua load async (anti force-close)

This commit is contained in:
Bro
2026-08-16 21:20:39 +07:00
parent 639f3a4ef6
commit cb22c1394a
6 changed files with 50 additions and 29 deletions
+35 -11
View File
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react';
import { Component, useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import { ActivityIndicator, StatusBar, StyleSheet, Text, View } from 'react-native';
import * as SplashScreen from 'expo-splash-screen';
import { SafeAreaProvider } from 'react-native-safe-area-context';
@@ -11,6 +12,27 @@ import { colors } from './src/theme/colors';
SplashScreen.preventAutoHideAsync().catch(() => {});
/** Tangkap error render agar tidak force-close: tampilkan layar aman. */
class ErrorBoundary extends Component<{ children: ReactNode }, { hasError: boolean; message: string }> {
state = { hasError: false, message: '' };
static getDerivedStateFromError(e: unknown) {
return { hasError: true, message: e instanceof Error ? e.message : String(e) };
}
render() {
if (this.state.hasError) {
return (
<View style={styles.center}>
<Text style={styles.errorTitle}>Terjadi kesalahan 😢</Text>
<Text style={styles.errorText}>{this.state.message}</Text>
</View>
);
}
return this.props.children;
}
}
export default function App() {
const [ready, setReady] = useState(false);
const [error, setError] = useState<string | null>(null);
@@ -62,16 +84,18 @@ export default function App() {
}
return (
<SafeAreaProvider>
<NavigationContainer>
<RootNavigator initialRoute={initialRoute} />
</NavigationContainer>
<StatusBar
barStyle="light-content"
translucent={false}
backgroundColor={colors.primary}
/>
</SafeAreaProvider>
<ErrorBoundary>
<SafeAreaProvider>
<NavigationContainer>
<RootNavigator initialRoute={initialRoute} />
</NavigationContainer>
<StatusBar
barStyle="light-content"
translucent={false}
backgroundColor={colors.primary}
/>
</SafeAreaProvider>
</ErrorBoundary>
);
}