React Native-18.React Native 常用API及实践 NetINfo
来源:互联网 发布:windows无法启动怎么办 编辑:程序博客网 时间:2024/04/30 09:07
没图你说个xx
NetInfo简介
NetInfo 用来获取网络状态。
提供的属性方法如下:
- isConnected : 表示网络是否连接
- fetch(): 获取网络状态
- addEvent Li 沙特呢人(eventName,handler):添加事件监听。
- removeEventListener:(eventName,handler):删除事件监听。
其中,网络状态主要有以下几种类型。
- none:离线状态。
- wifi:在线状态。并且通过WiFi或者是iOS 模拟器连接。
- cell: 网络连接。通过3g,WiMax或者LTE进行连接。蜂窝网络。
- unknown:错误情况。网络状态未知。
直接上代码:
老样子,我们封装一个组件:
netinfo.js
var React = require('react-native')var { AppRegistry, StyleSheet, View, Text, NetInfo, TouchableOpacity, PixelRatio,} = React;var Netinfo = React.createClass({ getInitialState: function(){ return{ netstate:null } }, componentDidMount: function(){ // NetInfo.addEventListener('change',function(reachability){ // alert(reachability) // }); }, render:function(){ return( <View style= {styles.flex}> <TouchableOpacity style = {styles.borderStyle} onPress = {this.getCurrentNetInfo}> <Text style = {styles.font}> 获取当前网络状态 </Text> </TouchableOpacity> <TouchableOpacity style = {styles.borderStyle} onPress = {this.getCurrentNetState}> <Text style = {styles.font}> 获取当前网络是否连接 </Text> </TouchableOpacity> <TouchableOpacity style = {styles.borderStyle} onPress = {this.reachabilityNetState}> <Text style = {styles.font}> 监听网络是否链接 </Text> </TouchableOpacity> </View> ); }, getCurrentNetInfo: function(){ NetInfo.fetch().done(function(reachability){ alert(reachability); }); }, getCurrentNetState: function(){ NetInfo.isConnected.fetch().done(function(isConnected){ alert(isConnected); }); }, reachabilityNetState: function(){ NetInfo.isConnected.addEventListener('change',function(isConnected){ alert(isConnected); }); },});var styles = StyleSheet.create({ flex: { flex: 1, }, borderStyle: { marginRight: 10, marginLeft:10, marginTop:74, alignSelf:'center', borderWidth: 1/PixelRatio.get(), borderRadius: 5, borderColor: '#aaa', }, font: { fontSize:15, color: '#222', }})module.exports = Netinfo;
在os.index.js中我们这样做:
'use strict';var React = require('react-native');var Netinfo = require('./iOSFile/netinfo.js');var { AppRegistry, StyleSheet, Text, View, Image, ScrollView, WebView, NavigatorIOS, AsyncStorage, TouchableOpacity,} = React;var styles = StyleSheet.create({ container : { flex: 1 }, row : { flexDirection: 'row', marginBottom: 10, }, item : { flex: 1, marginLeft:5, borderWidth: 1, borderColor: '#ddd', marginRight: 5, height: 100, }, img: { flex: 1, backgroundColor: 'transparent', }, item_text: { backgroundColor: '#000', opacity:0.7, color:'#fff', height:25, lineHeight:18, textAlign:'center', marginTop:74 }, btn: { backgroundColor: '#ff7200', height: 33, textAlign : 'center', color: '#fff', marginLeft:10, marginRight: 10, lineHeight: 24, marginTop: 40, fontSize: 18, }, list_item : { marginLeft: 5, marginRight: 5, padding:5 , borderWidth: 1, height: 30, borderRadius: 3, borderColor: '#ddd', }, list_item_desc : { flex: 2, fontSize: 15, }, list_item_price: { flex: 1, textAlign: 'right', fontSize: 15, }, clear: { marginTop : 10, backgroundColor: '#fff', color: '#000', borderColor: '#ddd', borderWidth:1, marginLeft: 10, marginRight:10, lineHeight: 24, height:33, fontSize: 18, textAlign: 'center', }});var wxsPrj = React.createClass({ render: function() { return ( <NavigatorIOS style = {styles.container} initialRoute = { { component:Netinfo, title:'样式列表', barTintColor: '#ddd' } }/> ); }});AppRegistry.registerComponent('wxsPrj', () => wxsPrj);
0 0
- React Native-18.React Native 常用API及实践 NetINfo
- react-native NetInfo
- React Native-13.React Native 常用API及实践 AppRegistry
- React Native-14.React Native 常用API及实践 AsyncStorage
- React Native-15.React Native 常用API及实践 AlertIOS AlertSheetIOS
- React Native-16.React Native 常用API及实践 PixelRatio
- React Native-17.React Native 常用API及实践 AppStateIOS StatusBarIOS
- react native学习笔记20——常用API(2)AppState应用前后台、NetInfo网络状态
- React Native 实践小结
- 携程React Native实践
- React-Native实践
- Qunar React Native 实践
- react-native + redux 实践
- React Native 混合编程 之常用API
- react native常用modules
- React Native 常用控件
- [React-Native]常用组件
- React Native常用组件
- 对原型的个人理解
- C++服务器端+Android客户端的网络通信
- CSS层叠样式表——学习笔记
- C语言深度剖析-----函数
- svn文件回滚到某个历史版本
- React Native-18.React Native 常用API及实践 NetINfo
- LeetCode 8 String to Integer (atoi)
- JAVA实现在excel插入图片(POI模式)
- 主成分分析(Principal components analysis)-最大方差解释
- Exynos4412 Uboot 移植(六)—— 相关知识补充
- iOS mask 层和 CAShapeLayer层中间挖去一部分的 源码分析
- sendmail使用
- B+树索引算法
- 【nodejs】npm常用命令