仅当设备从脱机状态变为联机状态时才触发android'connectionChange'事件,反之亦然,但是在IOS中,它每次都在调用。
任何解决方法?
componentWillMount(){
NetInfo.isConnected.fetch().then(isConnected => {
if (isConnected) {
this.setState({
netConnection: isConnected
});
} else {
ToastAndroid.show("Connection TimeOut!", ToastAndroid.LONG);
this.setState({
netConnection: isConnected
});
}
});
NetInfo.isConnected.addEventListener(
"connectionChange",
this.handleFirstConnectivityChange
);
}
componentWillUnmount(){
NetInfo.isConnected.removeEventListener(
"connectionChange",
this.handleFirstConnectivityChange
);
}
handleFirstConnectivityChange = (isConnected)=>{
if (isConnected) {
ToastAndroid.show("Connection Is Live!", ToastAndroid.LONG);
this.setState({
netConnection: isConnected
});
} else {
ToastAndroid.show("Connection TimeOut!", ToastAndroid.LONG);
this.setState({
netConnection: isConnected
});
}
}