react-native - Net-info 发生错误在 React-Native 中不起作用
问题描述
我在我的代码中使用 Net-info 来检查互联网连接,但它对我不起作用。会报错。。
类型错误:未定义不是对象(正在评估'_reactNative.Netinfo.isConected')
我还在 AndroidManifest.xml 中设置了权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
但这对我不起作用
我的代码在这里..
import { StyleSheet, Text, View, NetInfo } from 'react-native';
export default class App extends Component{
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"})
}
});
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener(
'connectionChange',
this._handleConnectivityChange
);
}
_handleConnectivityChange = (isConnected) => {
if(isConnected == true)
{
this.setState({connection_Status : "Online"})
}
else
{
this.setState({connection_Status : "Offline"})
}
};
render() {
return (
<View style={styles.MainContainer}>
<Text style={{fontSize: 20, textAlign: 'center', marginBottom: 20}}> You are { this.state.connection_Status } </Text>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
padding: 20
},
TextStyle: {
fontSize:20,
textAlign: 'center'
}
});
如何解决这个问题请建议我任何解决方案。
解决方案
我不知道你有什么版本的 React Native,但 netinfo 已被提取到不同的库中。
https://github.com/react-native-community/react-native-netinfo
推荐阅读
- python - 如何使用 for 循环构建可以从列表中读取值的字典?
- sql - SQL插入接口切片
- python - 将 TwitterAPI 结果保存到 csv 文件
- python - 返回非实例化类类型的类型提示
- android - 备份时的Android 9数据库日志模式WAL问题
- javascript - 如何在同一个 div 中使用 jquery hover 来处理 div 和 html 标签
- count - Tableau - 计算最大数量
- c++ - C++ 中是否有 '_itoa_s' 函数的替代品
- c# - 如果仅更改名称,如何从方法派生?
- javascript - 为什么我的 BMI 计算器显示错误的 BMI?