开发一个基于React Native的简易demo--读取网络数据并展示
2017-12-07 11:23
721 查看
react-native的网络请求用fetch,及其简单,请求到的数据保存起来,react-native用state来保存数据,类似于Java的request,还可以传递给另一个类,所以就是:请求数据,赋值。展示数据这里用ListView,有点类似于Java的HashMap,要求唯一的key,一个key代表一条数据。
定义一个方法,接收fetch的数据,并赋值给state中的dataSource:
由于ListView会跟Swiper有滑动冲突,所以,在用ListView渲染数据时,要做个判断componentDidMount(),等ListView数据加载完,再启用Swiper:
定义一个方法,接收fetch的数据,并赋值给state中的dataSource:
buttonTap=()=>{ fetch( 'http://bbs.reactnative.cn/api/category/3' ).then((response)=>response.json()) .then((jsondata) =>{ const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.setState({dataSource: ds.cloneWithRows(jsondata.topics)}); this.setState({title:jsondata.description}); //alert(jsondata); }) .catch((error)=>{ alert(error); console.warning(error); }); };
由于ListView会跟Swiper有滑动冲突,所以,在用ListView渲染数据时,要做个判断componentDidMount(),等ListView数据加载完,再启用Swiper:
class RecentChatsScreen extends React.Component { // render() { // return ( // <View style={[styles.containerSwiper]}> // <View style={{flexDirection:'column',justifyContent:'center',flex:1,alignItems:'center'}}> // <Text style={{color:'#fff',fontSize: 14,textAlign:'center'}}>当前尚未登录</Text> // <Text style={{color:'#fff',fontSize: 14,textAlign:'center'}}>请点击界面登录</Text> // </View> // </View> // ); // } // 初始化模拟数据 constructor(props) { super(props); const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.state = { swiperShow:false, dataSource: ds.cloneWithRows([ // 'John', 'Joel', 'James', 'Jimmy', 'Jackson', 'Jillian', 'Julie', 'Devin' ]) }; this.buttonTap();//初始化 } componentDidMount(){ setTimeout(()=>{ this.setState({ swiperShow:true }); },0) } buttonTap=()=>{ fetch( 'http://bbs.reactnative.cn/api/category/3' ).then((response)=>response.json()) .then((jsondata) =>{ console.log(jsondata); const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.setState({dataSource: ds.cloneWithRows(jsondata.topics)}); this.setState({title:jsondata.description}); //alert(jsondata); }) .catch((error)=>{ alert(error); console.warning(error); }); }; render() { if(this.state.swiperShow){ return( <View style={[styles.containerSwiper]}> <ListView style={{flex:8} } removeClippedSubviews={false} dataSource={this.state.dataSource} renderRow={(rowData) => <CELL title={rowData.title} detailTitle={rowData.timestampISO}></CELL>} enableEmptySections={true} /> </View> ) }else { return ( <View style={{height:100}}> </View> ); } } }
下一篇:开发一个基于React Native的简易demo–源码
相关文章推荐
- 开发一个基于React Native的简易demo--导航栏+轮播图
- 开发一个基于React Native的简易demo--前记
- 开发一个基于React Native的简易demo--视频组件+布局
- 开发一个基于React Native的简易demo--源码
- React Native第一个Demo(2)网络获取数据和ListView
- 一个简易的H5数据展示效果【demo】
- 基于react-redux开发一个待办事项的demo–todoList的笔记
- 一个简单的Android Native(C/C++)网络数据加密方案(附带demo)
- React-Native基础_5.列表视图ListView 网络数据展示
- 一个基于UDP数据广播的局域网络会议程序
- 一个大数据方案:基于Nutch+Hadoop+Hbase+ElasticSearch的网络爬虫及搜索引擎
- Android中基于HTTP的通信技术(1)使用Http的Get方式读取网络数据
- C#——正式开始自己的AE开发,先做一个button进行读取特定数据
- 基于Java NIO 异步读取网络数据
- Android一个Fragment展示列表网络数据
- 使用C#进行基于PI的开发(三)——应用PISDK和PIAPI从PI数据库读取数据
- Android中基于HTTP的通信技术(2)使用Http的Post方式读取网络数据
- 一个完整的Android ListView+网络接口读取+JSON处理的Demo
- 一个基于UDP数据广播的局域网络会议程序
- 基于VC++开发InlineHook网络数据发送接收函数