React Native-18.React Native 常用API及实践 NetINfo

前端之家收集整理的这篇文章主要介绍了React Native-18.React Native 常用API及实践 NetINfo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

没图你说个xx

NetInfo简介

NetInfo 用来获取网络状态。

提供的属性方法如下:
- isConnected : 表示网络是否连接
- fetch(): 获取网络状态
- addEvent Li 沙特呢人(eventName,handler):添加事件监听。
- removeEventListener:(eventName,handler):删除事件监听。

其中,网络状态主要有以下几种类型。
- none:离线状态。
- wifi:在线状态。并且通过WiFi或者是iOS 模拟器连接。
- cell: 网络连接。通过3g,WiMax或者LTE进行连接。蜂窝网络。
- unknown:错误情况。网络状态未知。

直接上代码

老样子,我们封装一个组件:
netinfo.js

  1. var React = require('react-native')
  2.  
  3. var {
  4. AppRegistry,StyleSheet,View,Text,NetInfo,TouchableOpacity,PixelRatio,} = React;
  5.  
  6. var Netinfo = React.createClass({
  7. getInitialState: function(){
  8. return{
  9. netstate:null
  10. }
  11. },componentDidMount: function(){
  12. // NetInfo.addEventListener('change',function(reachability){
  13. // alert(reachability)
  14. // });
  15. },render:function(){
  16. return(
  17. <View style= {styles.flex}>
  18. <TouchableOpacity
  19. style = {styles.borderStyle}
  20. onPress = {this.getCurrentNetInfo}>
  21. <Text style = {styles.font}>
  22. 获取当前网络状态
  23. </Text>
  24. </TouchableOpacity>
  25. <TouchableOpacity
  26. style = {styles.borderStyle}
  27. onPress = {this.getCurrentNetState}>
  28. <Text style = {styles.font}>
  29. 获取当前网络是否连接
  30. </Text>
  31. </TouchableOpacity>
  32. <TouchableOpacity
  33. style = {styles.borderStyle}
  34. onPress = {this.reachabilityNetState}>
  35. <Text style = {styles.font}>
  36. 监听网络是否链接
  37. </Text>
  38. </TouchableOpacity>
  39. </View>
  40.  
  41. );
  42. },getCurrentNetInfo: function(){
  43. NetInfo.fetch().done(function(reachability){
  44. alert(reachability);
  45. });
  46. },getCurrentNetState: function(){
  47. NetInfo.isConnected.fetch().done(function(isConnected){
  48. alert(isConnected);
  49. });
  50. },reachabilityNetState: function(){
  51. NetInfo.isConnected.addEventListener('change',function(isConnected){
  52. alert(isConnected);
  53. });
  54. },});
  55.  
  56. var styles = StyleSheet.create({
  57. flex: {
  58. flex: 1,},borderStyle: {
  59. marginRight: 10,marginLeft:10,marginTop:74,alignSelf:'center',borderWidth: 1/PixelRatio.get(),borderRadius: 5,borderColor: '#aaa',font: {
  60. fontSize:15,color: '#222',}
  61. })
  62.  
  63.  
  64. module.exports = Netinfo;

在os.index.js中我们这样做:

  1. 'use strict';
  2. var React = require('react-native');var Netinfo = require('./iOSFile/netinfo.js');
  3.  
  4. var {
  5. AppRegistry,Image,ScrollView,WebView,NavigatorIOS,AsyncStorage,} = React;
  6.  
  7.  
  8. var styles = StyleSheet.create({
  9. container : {
  10. flex: 1
  11. },row : {
  12. flexDirection: 'row',marginBottom: 10,item : {
  13. flex: 1,marginLeft:5,borderWidth: 1,borderColor: '#ddd',marginRight: 5,height: 100,img: {
  14. flex: 1,backgroundColor: 'transparent',item_text: {
  15. backgroundColor: '#000',opacity:0.7,color:'#fff',height:25,lineHeight:18,textAlign:'center',marginTop:74
  16. },btn: {
  17. backgroundColor: '#ff7200',height: 33,textAlign : 'center',color: '#fff',marginRight: 10,lineHeight: 24,marginTop: 40,fontSize: 18,list_item : {
  18. marginLeft: 5,padding:5,height: 30,borderRadius: 3,list_item_desc : {
  19. flex: 2,fontSize: 15,list_item_price: {
  20. flex: 1,textAlign: 'right',clear: {
  21. marginTop : 10,backgroundColor: '#fff',color: '#000',borderWidth:1,marginLeft: 10,marginRight:10,height:33,textAlign: 'center',}
  22.  
  23. });
  24.  
  25. var wxsPrj = React.createClass({
  26. render: function() {
  27. return (
  28. <NavigatorIOS style = {styles.container} initialRoute = { { component:Netinfo,title:'样式列表',barTintColor: '#ddd' } }/> ); } }); AppRegistry.registerComponent('wxsPrj',() => wxsPrj);

猜你在找的React相关文章