To run code in React Native only when a network connection is available, you can use the NetInfo module from the @react-native-community/netinfo package. This module allows you to subscribe to network connection changes and conditionally execute your code based on the current network status.
Source code viewer
import NetInfo from '@react-native-community/netinfo';
export async function internetConnected() {
// Run only when internet is connected.
const netInfo = await NetInfo.fetch();
if (!netInfo.isConnected) {
return;
}
// Code that you only run when network available, probably sync...
}Programming Language: ECMAScript