react-native-netinfo icon indicating copy to clipboard operation
react-native-netinfo copied to clipboard

Connection on background / locked screen (no internet connection with a stable internet connection.)

Open MayaZeynalovaDA opened this issue 2 years ago • 8 comments

After returning from the background isConnected mostly returns false firstly and after a while returns true.

Its happening in IOS and Android real device connected to Wifi

Code:

  useEffect(() => {
    const removeNetInfoSubscription = NetInfo.addEventListener(state => {
      const isConnected = state.isConnected ?? true;
      const isInternetReachable = state.isInternetReachable ?? true;
      const offline = !isConnected || !isInternetReachable;
      setNetworkConnectionOffline(offline);
    });

    return () => removeNetInfoSubscription();
  }, []);

MayaZeynalovaDA avatar May 26 '23 09:05 MayaZeynalovaDA

the same on both iOS and android

@react-native-community/netinfo: 9.3.4 react-native: 0.71.0

vladimir-vasilev avatar Jun 05 '23 15:06 vladimir-vasilev

Any updates on this one?

ValeriyaZavozova avatar Jun 26 '23 13:06 ValeriyaZavozova

@ValeriyaZavozova in an open source repository, "updates" are visible to all (so you'd see them if they existed), but do not typically create progress unless they contain the magic phrase "I've investigated this and posted a PR here <link to PR>"

mikehardy avatar Jun 29 '23 16:06 mikehardy

Still an issue. Been digging into this but no luck.

tomgreco avatar Aug 01 '23 16:08 tomgreco

My problem seemed to be that I was doing ->

useEffect(() => {
    const unsubscribe = NetInfo.addEventListener((state) => {
    });
    return unsubscribe
  }, []);

But this worked ->

  useEffect(() => {
    const unsubscribe = NetInfo.addEventListener((state) => {
    });
    return () => {
      unsubscribe();
    };
  }, []);

Its still a bit slow to change the state sometime and debugging the simulator or a build to a device was difficult because when the internet is turned off the connection to the debugger went off.

pajdak3 avatar Aug 24 '23 16:08 pajdak3