devedv
devedv

Reputation: 622

NetInfo returns true when connected to wifi but no internet

NetInfo returns true when connected to wifi but there is no internet. So apart from NetInfo what are the other best options to check internet connectivity in react native?

Upvotes: 1

Views: 2506

Answers (2)

flaky
flaky

Reputation: 7404

unfortunately there is no alternative to pinging some kind of host and wait for the response.

You could do something like this:

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

Hope this helps

Upvotes: 2

csath
csath

Reputation: 1316

You have to ping and see if connection actually has internet connection. First use netInfo. Using it's icConnected method find out if the device is connected to a network. If so use your HTTP client to add an interceptor and try to ping and check internet connectivity before sending the actual request.

Also if you need more advanced stuff try out this library to work with offline data. It'll solve your problem. https://github.com/rauliyohmc/react-native-offline

Upvotes: 4

Related Questions