Description:
When the app is running in a background state or Locked, When the user unlock or foreground the app, useNetInfo(); hook return as isConnected as false. Even I tried to re-fetch the state still using NetInfo.fetch() return the same state.
It's happening in Android real device connected to Wifi
Package Name: "@react-native-community/netinfo": "^7.1.2",
Code:
const netInfo = useNetInfo();
const [show, setShow] = useState(false);
useEffect(() => {
setShow(!(netInfo.isConnected && netInfo.isInternetReachable));
}, [netInfo]);
useEffect(() => {
fetchConnection();
}, []);
const fetchConnection = () => {
NetInfo.fetch().then((state: any) => {
setShow(!(state.isConnected && state.isInternetReachable));
});
};
I fixed this issue by reverting the package version into
"@react-native-community/netinfo": "5.9.7", Also, change theandroidXCoreversion into 1.6.0. Now it's working as expected.Reason: Due to the hibernation features changes in
androidXCoreversion 1.7.0.Netinfodoes not return the state properly if the app is in a hibernation state. Please fix this issue in the upcoming release. Thanks.