这是您当前使用的代码来交换和验证连接,我认为它正常工作:
constructor() {
super();
this.state = {
connection_Status: ""
}
}
componentDidMount() {
NetInfo.isConnected.addEventListener(
'connectionChange',
this._handleConnectivityChange
);
NetInfo.isConnected.fetch().done((isConnected) => {
if (isConnected == true) {
this.setState({ connection_Status: "Online" })
}
else {
this.setState({ connection_Status: "Offline." })
}
});
SplashScreen.hide();
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener(
'connectionChange',
this._handleConnectivityChange
);
}
_handleConnectivityChange = (isConnected) => {
if (isConnected == true) {
this.setState({ connection_Status: "Online" })
}
else {
this.setState({ connection_Status: "Offline." })
}
};
如何检查URL?我考虑了现在使用它,但是我无法实施
let req = await fetch('https://www.someurl.com'); let isConnected = req.status === 200;
对不起,我是新手反应的
提取返回承诺,
因此,您可以这样使用它,
fetch('https://www.someurl.com')
.then((res) =>{
this.setState({status:res.status});
})
.then((res) => {
//do somthing with responce
})
.catch((error) => {
console.error(error);
});