在react native中使用fetch处理错误请求和网络错误时应用程序崩溃



当fetch返回错误请求或网络错误时,我的react原生应用程序在.apk中崩溃以下是提取功能:

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);
}
}```

首先检查API响应是否返回空或未定义的内容,然后在屏幕中使用该内容。例如,当我们使用flatlist并传递undefined和null时,apk崩溃,所以请关注您的数据格式

最新更新