React-native 利用NetInfo获取网络状态

适用于双平台网络状态检测

NetInfo.isConnected.fetch().done((isConnected) => {

console.log('First, is ' + (isConnected ? 'online' : 'offline'));

});

function handleFirstConnectivityChange(isConnected) {

console.log('Then, is ' + (isConnected ? 'online' : 'offline'));

 if (!isConnected) {

   AlertIOS.alert("网络错误", "网络连接失败,请连接网络后重试!");   

 }

NetInfo.isConnected.removeEventListener(

    'change',

     handleFirstConnectivityChange

    );

}

NetInfo.isConnected.addEventListener(

    'change',

    handleFirstConnectivityChange

);

你可能感兴趣的:(React-native 利用NetInfo获取网络状态)