如何处理网络故障的反应-本机,当设备没有连接到网络.
我的方案是尝试连接一些api,而如果网络断开,则获取请求--本机抛出网络请求失败错误。如何处理此场景以给用户提供最佳的用户体验。
如何更好地处理网络请求失败。

发布于 2016-04-29 10:21:45
像这样使用NetInfo:
// Check for network connectivity
NetInfo.isConnected.fetch().done((isConnected) => {
if ( isConnected )
{
// Run your API call
}
else
{
// Ideally load from local storage
}
});发布于 2019-02-07 07:23:49
使用catch块处理异常,可以从catch块执行处理异常的操作,可能是重定向或状态更改以显示错误。
const url = `your url to be fetched`;
fetch(url, {
method: "GET",
headers: header
})
.then(res => res.json())
.then(json => {
console.log(json);
this.setState({
data:json
});
})
.catch(error => {
if (error) {
//perform the redirection to the network request slow page or set state to show error
}
});发布于 2021-06-30 16:34:14
这可能是因为两件主要的事情:
AndroidManifest.xml或info.plist)只要在catch里面处理这个案子
try {
const response = fetch(request)
} catch (e) {
if (error.message === 'Network request failed') {
// retry or any other handling like showing alert
} else {
throw e;
}
}https://stackoverflow.com/questions/36927007
复制相似问题