vaklinzi
vaklinzi

Reputation: 2003

Check for internet connection returns wrong results

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?

Upvotes: 5

Views: 2788

Answers (1)

Ben Clayton
Ben Clayton

Reputation: 82209

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.

Upvotes: 6

Related Questions