'App crushes on bad request and network error using fetch in react-native
My react native app crashes in .apk when fetch returns a bad request or network error Below is the fetch function:
try {
const reponse = await fetch(
'http://example.com',
{
method: 'POST', // *GET, POST, PUT, DELETE, etc.
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body
},
);
if (reponse.ok) {
const data = await reponse.json();
console.warn('Success response', data);
return navigation.navigate('different', {
token: data.token,
memberNo: data.user.memberno,
});
} else {
setStatus('Incorrect Details entered');
// console.warn('Failed response', reponse);
}
} catch (error) {
setStatus('Network request failed connect to the internet');
// console.error('CATCH Error', error);
}
}```
Solution 1:[1]
First check you API response may be its return something null or undefined and you used that in your screen. e.g when we use flatlist and pass undefined and null then the apk crash so focus on your data format
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad Komail Abbas |
