react-native - How do I specifically check a URL with NetInfo React-Native?
问题描述
This is the code you are currently using to exchange and verify a connection, I think it is working fine:
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." })
}
};
How do I check a URL? I thought about using it now, but I could not implement it
let req = await fetch('https://www.someurl.com'); let isConnected = req.status === 200;
sorry for this, i'm new with reacting native
解决方案
fetch 返回承诺,
因此你可以像这样使用它,
fetch('https://www.someurl.com')
.then((res) =>{
this.setState({status:res.status});
})
.then((res) => {
//do somthing with responce
})
.catch((error) => {
console.error(error);
});
推荐阅读
- xpath - 使用 Scrapy 提取不带标签的文本序列
- json - 基于 ENUM 值的验证 JSON 子模式定义
- entity-framework - Dynamic list using array from anthor list
- opendaylight - 您可以编写脚本来修改 SDN 控制器的流表吗?
- python - 虽然 opencv 安装成功,但我收到错误“ModuleNotFoundError: No module named 'cv2'”
- php - 多维数组中的unset()首先检查键是否存在
- javascript - React Context API:提供者不提供
- kotlin - 在 Kotlin 中退出 onClickListener
- c# - Firebase 主题消息传递中不可预测的服务行为
- r - 将两组列名传递给函数