When Using Fetch, Axios, Etc.`catch` Doesn't Handle The Error, Instead Your App Crashes
To make sure that our request will be successful, first, we check the internet connection then send our request. like this: NetInfo.isConnected.fetch().then(async isConnected=>
Solution 1:
Actually this is a mistake on our side, react-native will crash as it encounters console.error
.
so by changing the above code to this version you will get rid of the red screen:
NetInfo.isConnected.fetch().then(async isConnected=> {
if(isConnected){
try {
let result = await fetch(MY_REMOTE_SERVER);
console.log("result: ", result)
} catch (error) {
// use "log" instead of "error"
console.log("error: ", error);
// or you may want to show a toast on error like
oastAndroid.show('No internet', ToastAndroid.SHORT)
}
}else ToastAndroid.show('No internet', ToastAndroid.SHORT);
});
Post a Comment for "When Using Fetch, Axios, Etc.`catch` Doesn't Handle The Error, Instead Your App Crashes"