react-native ios netInfo表现奇怪

时间:2018-06-26 10:18:56

标签: ios react-native

仅当设备从脱机状态变为联机状态时才触发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
        });
    }
}

0 个答案:

没有答案