1

Hi I working on a react native project I need to support my app offline. I used NetInfo Library from expo documentation.
like below

  const netInfo = useNetInfo();
const networkCheck = () => {
    setTimeout(() => {
      const net = netInfo.isInternetReachable;
      console.log(net);
      if (net === false) {
        setloadCachedListings(true);
      }
    }, 2000);
  };
  networkCheck();

I want to wait for some time at this step of my code because this hook always returns null first time then after few milliseconds it tells the real network status. But this code is not working as I an trying to do. Is there any way to achieve this? I just want to wait a little bit to get real network connection and then go further with my code.

logs for netInfo hook. enter image description here

Saad Ramay
  • 152
  • 1
  • 11
  • Looks like netinfo is a Promise – Sebastián Espinosa May 13 '21 at 22:42
  • It return object four times when page is loaded. First time its pinging with servers to check for network status and then ultimately shows the real info in fourth object – Saad Ramay May 13 '21 at 22:46
  • @SebastiánEspinosa I have added the logs for useNetInfo hook. – Saad Ramay May 13 '21 at 22:50
  • well, start by changing the `if (net === false) {` to `if (!net) {`, because for what I read that function returns `null` instead of `false` – Sebastián Espinosa May 14 '21 at 06:14
  • Did you ever find a solution for this? @SebastiánEspinosa, your solution doesn't work becuause the idea is that we want to give it 4 or 5 seconds (not the default 20-25 seconds) to process it and otherwise show a no network page (for example) whereas with a null check it will go to the no internet page even if it take 0.2 seconds to load. – Liam Clark Gutiérrez Mar 09 '23 at 22:19

0 Answers0