我正在尝试在我的 React Native 应用程序中创建一个健壮的模式,以便如果没有 Internet 连接,我不会进行 API 调用,而是显示“连接丢失...”消息。
我创建了以下 util 函数并尝试将其合并到我的 API 调用中,但在得到响应后fetch
似乎没有遇到问题。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
障碍或建议使它成为一个健壮的模式吗?