Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for internet connection returns wrong results

Tags:

react-native

I am trying to check whether the user is connected to internet. I am using NetInfo like this (from the documentation):

componentDidMount() {
    NetInfo.isConnected.addEventListener('change', this.handleConnectionChange);

    NetInfo.isConnected.fetch().done(
      (isConnected) => { this.setState({ status: isConnected }); }
    );
}

componentWillUnmount() {
    NetInfo.isConnected.removeEventListener('change', this.handleConnectionChange);
}

handleConnectionChange = (isConnected) => {
        this.setState({ status: isConnected });
        console.log(`is connected: ${this.state.status}`);
}

The strange is that the first time loading the screen where I am doing this is working fine. But when I start turning on/of my wifi the results are different: sometimes it detects the change sometime no. Someone having the same issue?

like image 332
vaklinzi Avatar asked Mar 07 '17 06:03

vaklinzi


People also ask

How do I troubleshoot my network connection?

And if you're having trouble with your connection, you can run the Network troubleshooter to try and fix it. Select the Start button, then select Settings > Network & Internet > Status. Check your network connection status

How do I Check my network connection status?

Check your network connection status. Windows 10 lets you quickly check your network connection status. And if you're having trouble with your connection, you can run the Network troubleshooter to try and fix it. Select the Start button, then select Settings > Network & Internet > Status. Check your network connection status.

Why can’t I detect the Internet on my Network?

In case active probing fails, passive probing can also detect internet unless it’s disabled by GPO. Check below to make sure you didn’t disable passive probing. If it is, enable it and test again. Group Policy path: Computer Configuration\Administrative Templates\Network\Network Connectivity Status Indicator

How do I know if my Wi-Fi Connection is disconnected?

Test your Wi-Fi on different devices If you’re on your laptop, pull out your phone or tablet to see if you can get internet over the same Wi-Fi connection. If it connects on one device but not the other, then you know that your internet connection is fine and it’s the disconnected device that’s having the problem.


1 Answers

In my experience, the iOS simulator doesn't 'notice' when the internet connection is re-connected when using the React Native NetInfo class.

It is rather annoying. However, for me it works as intended on a real device.

like image 52
Ben Clayton Avatar answered Sep 18 '22 21:09

Ben Clayton