question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: isConnected/isInternetReachable should NOT return false on first render.

See original GitHub issue

Describe the Feature

Current implementation of the useNetInfo hooks is kind of weird. Especially for slow connection. By default this 2 flags return false on the first render. If you have a naive component that use this to do some kind of

if (isInternetReachable) return <ErrorMessage />
else return <ActualComponentBody thatRequires="internet" />

You have for a very short moment, an error message. I would expect this hook instead to return “undefined” when things are actually unknown, instead of a false negative.

Currently if you do a native call of the hook & use your app in a poorly connected env, you can have a “false” for a few hundreds ms, which can show an error.

Instead of doing

  • false
  • then listen

I would do

  • undefined
  • fetch & update
  • then listen

Possible Implementations

Here is something I am using. Adjusted for my needs as I only use isInternetReachable.

// @flow

// https://github.com/react-native-netinfo/react-native-netinfo/issues/405

import * as React from 'react';
import NetInfo from '@react-native-community/netinfo';
import type { NetInfoState } from '@react-native-community/netinfo';

export default (): void | boolean => {
  const [isInternetReachable, isInternetReachable_set] = React.useState(
    undefined,
  );

  React.useEffect(() => {
    const update = (state: NetInfoState) => {
      // ignore first initialisation on iOS where it's null for a moment
      if (
        isInternetReachable === undefined &&
        state.isInternetReachable === null
      ) {
        return;
      }
      if (state.isInternetReachable !== isInternetReachable) {
        isInternetReachable_set(() => state.isInternetReachable);
      }
    };
    NetInfo.fetch().then(update);
    return NetInfo.addEventListener(update);
  }, [isInternetReachable]);

  return isInternetReachable;
};

Related Issues

I detected a few issue that could be solved by a new implementation as I am recommending like #356, #254 and #295 and maybe indirectly issues like #352, #326

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:25
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
mikehardycommented, Sep 14, 2020

@EhsanSarshar it’s open source, why wouldn’t you just PR something more reliable?

1reaction
TheEhsanSarsharcommented, Sep 14, 2020

or you can do.

NetInfo.fetch().then((state: NetInfoState) => {
  if(state.isInternetReachable === null) return
  if (state.isInternetReachable !== isInternetReachable) {
        isInternetReachable_set(() => state.isInternetReachable)
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check the internet reachability in React-native?
I've tried @react-native-community/netinfo to check the internet reachability. But the scenario I want to implement is, suppose if my device is ...
Read more >
Managing network connection status in React Native
The first step of this plan should be letting the user know that their device is not connected to the internet; it is...
Read more >
https://gitlab.parity.io/parity/mirrors/parity-sig...
s||fe++,fe>1&&console.warn('In most cases you should not have more ... .unknown,isConnected:!1,isInternetReachable:!1,details:null}),n=(0,u.default)(t,2) ...
Read more >
159.203.103.78/himanshutank/pinal-mobile-react-nat...
applyWithGuard(n,r,t),null)},inGuard:function(){return!!r},guard:function(n,r,t){var u;if('function'!=typeof n)return console.warn('A function must be ...
Read more >
react-native-animatable | Standard set of easy to use animations
'bounce finished' : 'bounce cancelled')); render() { return ( <TouchableWithoutFeedback ... TypeError: stateInfo.map is not a function react-native.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found