【React Native开发】
来源:互联网 发布:网络微信图片发不出去 编辑:程序博客网 时间:2024/05/21 08:57
React Native 使用fetch进行网络请求
1.介绍
Fetch 请求网络的方式和web请求网络的方式是一致的,可用于满足开发者访问网络的需求。
2.get请求
2-1 在构造方法中初始化数据
constructor(props){ super(props) this.state = { dataSource:new ListView.DataSource({ rowHasChanged:((row1,row2) => (row1 !== row2)) }), load:false }}2-2 在componentDidMount()方法中请求网络,获得数据源componentDidMount()页面加载完成后执行
getData(){ fetch('http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/2',{ method:'GET',//get请求 }) .then((response) => response.json())//获取数据 .then((responseText) => {//处理数据 this.setState({//刷新界面 load:true, dataSource:this.state.dataSource.cloneWithRows(responseText.results) }) }) .catch((error) => {//获取数据错误时执行 console.warn(error) }).done()}Json 数据2-3 渲染界面render(){ if (!this.state.load){ return this.renderLoading() } return this.renderListView(this.state.dataSource) }//页面加载时执行renderLoading(){ return ( <View style={styles.load}> <Text>正在加载中...</Text> </View> )}//数据加载完毕,获取数据时执行renderListView(dataSource){ return ( <ListView dataSource={dataSource} renderRow={this.renderRow}> </ListView> )}//渲染界面 renderRow(rowData){ return ( <View style={styles.container}> <Image source={{ uri: rowData.url }} style={{ width: width, height: height / 2, marginTop: 5 }}></Image> </View> )}2-4 样式const styles = StyleSheet.constructor({ container:{ flex:1, }, load:{ flex:1, justifyContent:'center', alignItems:'center', },})2-5 效果:
阅读全文
1 0
- React Native开发
- React Native 开发笔记
- React-Native开发app
- react-native开发快捷键
- React Native开发(一)
- React Native开发小结
- React Native开发(二)
- React Native开发(一)
- React Native 开发
- 【React Native开发】
- 【React Native开发】
- 【React Native开发】
- 【React Native开发】
- 【React Native开发】
- 【React Native开发】
- React Native开发APP
- vscode react native 开发
- 【React Native开发】
- java获取使用mybatis刚插入数据的主键id
- has_secure_password可以使用在update上的原因
- Fragment的动态添加
- java.security.NoSuchAlgorithmException: AES KeyGenerator not available
- 2017全国信号处理技术应用大会暨2017中国信号处理技术工程师年会
- 【React Native开发】
- lua_rawget,lua_rawset,lua_rawgeti,lua_rawseti
- git上传本地单独修改的文件
- 调用CSDN的开发API——>博客接口——>获取博客系统分类【1】
- 两种表示异常类的方法
- http://www.5yun.org/13130.html
- Django中文官方版05-配置后台管理
- js对数组元素的操作-增删改查
- 跨VLAN远程开机功能实现