Cry*_*fel 2 android ios react-native
在本机反应中,有没有办法获取设备的网络状态?例如,我想知道当前设备是在线还是离线.此外,我想检测设备何时联机或脱机,以便向用户或我需要运行的任何其他进程显示消息.
到目前为止,我所能找到的只是向API发送请求,并设置catch回调fetch,如果没有连接catch回调将运行,这似乎工作,但我想知道是否有更好的方法来解决这个问题.
我还想知道该设备是否使用wifi或lte连接.
有帮助吗?
看看React Natives NetInfo API.以下示例是从官方文档中复制的:
const ConnectionInfoCurrent = React.createClass({
getInitialState() {
return {
connectionInfo: null,
};
},
componentDidMount: function() {
NetInfo.addEventListener(
'change',
this._handleConnectionInfoChange
);
NetInfo.fetch().done(
(connectionInfo) => { this.setState({connectionInfo}); }
);
},
componentWillUnmount: function() {
NetInfo.removeEventListener(
'change',
this._handleConnectionInfoChange
);
},
_handleConnectionInfoChange: function(connectionInfo) {
this.setState({
connectionInfo,
});
},
render() {
return (
<View>
<Text>{this.state.connectionInfo}</Text>
</View>
);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1439 次 |
| 最近记录: |