0

I use 99% on my screens fetch. So if the connection is lost and data are not fetched my screen is empty. So when the internet is again online nothing happens.

So then I downloaded netInfo but if I disable my internet connection then my isOffline state is always false. Nothing happens, how can I detect on real time when internet is off/on?

  const [isOffline, setIsOffline] = useState(false);
  useEffect(() => {
      const removeNetInfoSubscription = NetInfo.addEventListener((state: NetInfoState) => {
        const offline = !(state.isConnected && state.isInternetReachable)
        console.log(offline)
        setIsOffline(offline)
      })
  
      return () => removeNetInfoSubscription()
    }, [])

    console.log(isOffline);
localdata01
  • 587
  • 7
  • 17
  • Does this answer your question? [Detect network connection in React Redux app - if offline, hide component from user](https://stackoverflow.com/questions/40248639/detect-network-connection-in-react-redux-app-if-offline-hide-component-from-u) – Lenny4 Dec 16 '22 at 21:01

1 Answers1

0

If you are using an old version of React Native, you might face this issue with netinfo. Downgrading the version of netinfo to v6 (or updating the React Native to version>=0.65) might make it work.

See this GitHub issue

  • I use it with expo so I can not downgrade the netInfo version or rn version... – localdata01 Dec 17 '22 at 22:44
  • Hmm, can you try this: https://stackoverflow.com/a/61698953/3933853. This person seems to be downgrading netinfo version on Expo. – haidernawaz99 Dec 18 '22 at 07:05
  • No he writes not with expo^^ but thanks for the answer – localdata01 Dec 18 '22 at 07:22
  • I believe the first fix: changing the version in package.json and then using npm install command should work, even in expo. Expo install also uses npm install to install a compatible version. https://forums.expo.dev/t/difference-expo-install-versus-npm-install/31388/3 – haidernawaz99 Dec 18 '22 at 07:40
  • Also: you can most definitely update the version of react-native by upgrading the expo sdk version. https://docs.expo.dev/workflow/upgrading-expo-sdk-walkthrough/ – haidernawaz99 Dec 18 '22 at 07:48