React native 当连接到wifi但没有互联网时,NetInfo返回true

React native 当连接到wifi但没有互联网时,NetInfo返回true,react-native,React Native,当连接到wifi但没有internet时,NetInfo返回true。那么,除了NetInfo之外,在react native中检查internet连接的其他最佳选项是什么呢?不幸的是,除了ping某种主机并等待响应之外,别无选择 你可以这样做: poll() { setTimeout(() => { poll(); return fetch('http://www.google.com') .then((response) => {

当连接到wifi但没有internet时,NetInfo返回true。那么,除了NetInfo之外,在react native中检查internet连接的其他最佳选项是什么呢?

不幸的是,除了ping某种主机并等待响应之外,别无选择

你可以这样做:

poll() {
  setTimeout(() => {
    poll();

    return fetch('http://www.google.com')
      .then((response) => {
        // this is the success callback
        // you could check if the app was previously offline
        // and set it back to online to get rid of the offline hint
      })
      .catch((err) => {
        // error callback
        // use this to handle some sort of notification for the user
        // you could update a state within your root container
        // or redux to update the app with some visual hints
      });
  }, 5000);
}

希望这有帮助

不幸的是,除了ping某种主机并等待响应之外,别无选择

你可以这样做:

poll() {
  setTimeout(() => {
    poll();

    return fetch('http://www.google.com')
      .then((response) => {
        // this is the success callback
        // you could check if the app was previously offline
        // and set it back to online to get rid of the offline hint
      })
      .catch((err) => {
        // error callback
        // use this to handle some sort of notification for the user
        // you could update a state within your root container
        // or redux to update the app with some visual hints
      });
  }, 5000);
}

希望这对您有所帮助

您必须ping并查看连接是否确实具有internet连接。 首先使用netInfo。使用它的icConnected方法查找设备是否连接到网络。如果是这样,请使用HTTP客户端添加拦截器,并在发送实际请求之前尝试ping和检查internet连接

此外,如果您需要更高级的资料,请尝试使用此库来处理脱机数据。这会解决你的问题。

您必须ping并查看连接是否实际具有internet连接。 首先使用netInfo。使用它的icConnected方法查找设备是否连接到网络。如果是这样,请使用HTTP客户端添加拦截器,并在发送实际请求之前尝试ping和检查internet连接

此外,如果您需要更高级的资料,请尝试使用此库来处理脱机数据。这会解决你的问题。

谢谢!你能详细说明我如何使用离线状态吗。我无法执行此操作。catch(('offline')=>{alert(offline)})哦,对不起,这里的意思是
是伪代码,我会更新示例谢谢您的编辑,但是您如何判断在catch块中输入的获取请求仅仅是因为没有internet?可能是其他问题,例如web api服务器无法访问或类似的问题。是的,可能是这样,如果google服务器关闭-随着服务器正常运行时间的延长,这是您的最佳选择。我的意思是,如果你想让它“防弹”,你可以检查几个主机,使用聚合结果加上netinfo的内容,并且很有可能知道你是否连接到internet。随着服务器正常运行时间的推移,我想可以非常肯定的是,谷歌不会崩溃。@devedv可以随意将其标记为正确答案:)谢谢!你能详细说明我如何使用离线状态吗。我无法执行此操作。catch(('offline')=>{alert(offline)})哦,对不起,这里的意思是
是伪代码,我会更新示例谢谢您的编辑,但是您如何判断在catch块中输入的获取请求仅仅是因为没有internet?可能是其他问题,例如web api服务器无法访问或类似的问题。是的,可能是这样,如果google服务器关闭-随着服务器正常运行时间的延长,这是您的最佳选择。我的意思是,如果你想让它“防弹”,你可以检查几个主机,使用聚合结果加上netinfo的内容,并且很有可能知道你是否连接到internet。随着服务器正常运行时间的推移,我想可以非常肯定的是,谷歌不会崩溃。@devedv可以随意将其标记为正确答案:)更新了答案。更新了答案。