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?
source
share