Reputation: 2322
I'm using react-native-netinfo
to keep track of my device's internet connection. It was working fine for the most part until the point where the internet is disconnected and comes back on. At this point NetInfo
does not respond with the correct internet state.
While going over the repo on github it looks like this is a known issue without a fix yet. In one of the threads, there was a mention of using the hook useNetInfo
instead cos apparently that still worked fine.
So I tried that out as mentioned here https://github.com/react-native-netinfo/react-native-netinfo/issues/400#issuecomment-953177841 but even this isn't returning the correct state when the connection is established again.
I'm not sure what to do now. I don't know if there is more to this hook or if it needs to be implemented differently.
Upvotes: 2
Views: 2263
Reputation: 1248
What I did to make it work was to add an event listener in componentDidMount() and remove the listener in componentWillUnMount().
componentDidMount(){
NetInfo.isConnected.fetch().then(isConnected => {
this.handleConnectivityChange(isConnected === undefined ? true : isConnected);
});
NetInfo.isConnected.addEventListener(
'connectionChange',
this.handleConnectivityChange
);}
My handleConnectivityChange() function stores the isConnected boolean in redux to be used wherever I want.
componentWillUnMount() {
NetInfo.isConnected.removeEventListener(
'connectionChange',
this.handleConnectivityChange
);
}
Upvotes: 1