【发布时间】:2020-07-22 17:08:51
【问题描述】:
我正在尝试在我的 React Native 应用程序中创建一个健壮的模式,以便如果没有 Internet 连接,我不会进行 API 调用,而是显示“连接丢失...”消息。
我创建了以下 util 函数并尝试使用 fetch 将其合并到我的 API 调用中,但在收到响应后它似乎没有命中 then 块。
这是检查连接的函数:
import NetInfo from "@react-native-community/netinfo";
export const isConnectedToInternet = () => {
NetInfo.fetch().then(state => {
return state.isConnected;
});
};
这就是我尝试使用它的方式:
import { isConnectedToInternet } from '../my-utils';
export const someApiCall = () => {
isConnectedToInternet()
.then(result => {
if(result) {
return (dispatch) => fetch ('https://myapi/someendpoint', fetchOptionsGet())
.then(response => {
// Process data...
})
} else {
Alert.alert('No Internet connection!');
}
})
};
知道为什么我没有点击then 块或建议使这成为一个健壮的模式吗?
【问题讨论】:
-
isConnectedToInternet不会从您的样本中返回任何内容
标签: javascript react-native react-native-community-netinfo