I'm trying to detect if user is connected to the internet. My problem occurs when user is connected to wifi but still have access to mobile internet, then when user turn off wifi (still connected to mobile internet) isInternetReachable state changes to false and then back to true. Anyone encountered something simiral and got some solution for this? I'm not testing on emulator btw.
export function useNetworkHook() {
const [network, setNetwork] = useState(true);
useEffect(() => {
const unsubscribe = NetInfo.addEventListener(data =>
handleConnectivityChange(data.isConnected),
);
return () => {
unsubscribe;
};
}, []);
function handleConnectivityChange(isConnected: boolean | null) {
if (isConnected === null || isConnected === true) {
setNetwork(true);
} else {
setNetwork(isConnected);
}
}
return {network};
}