Testing your Internet connection returns incorrect results

I am trying to check if the user is connected to the 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}`);
}

It is strange that when I first load the screen, where I do it, it works fine. But when I turn on / my wifi, the results are different: sometimes it detects a change, once there is none. Does anyone have the same problem?

+4
source share
1 answer

In my experience, the iOS simulator does not "notice" when the Internet connection is reconnected when using the React Native NetInfo class.

This is pretty annoying. However, for me it works like on a real device.

+5
source

Source: https://habr.com/ru/post/1671675/


All Articles