React-Native学习笔记 使用ListView加载网络数据
2017-04-08 18:42
841 查看
使用ListView加载网络数据
本文采用fecth来进行网络请求的创建一个构造器
//js组件的构造函数,js的生命周期 constructor(props) { super(props); //listView数据源 dataSource: new ListView.DataSource({ rowHasChanged: function (row1, row2) { return row1 !== row2 }, sectionHeaderHasChanged:function (s1, s2) { return s1 !== s2 }, }), //网络请求状态 error: false, errorInfo: "" }; }
创建网络请求
var REQUEST_URL = 'https://api.github.com/search/repositories?q=javascript&sort=stars'; fetchData() { //这个是js的访问网络的方法 fetch(REQUEST_URL) .then((response) => response.json()) .then((responseData) => { this.setState({ //复制数据源 dataSource: this.state.dataSource.cloneWithRows(responseData.items) }); }) .catch((error) => { this.setState({ error: true, errorInfo: error }) }) .done();
创建Loading View
//加载等待的view renderLoadingView() { return ( <View style={styles.container}> <Text> Loading start... </Text> </View> ); }
创建请求失败时显示的view
//加载失败view renderErrorView(error) { return ( <View style={styles.container}> <Text> Fail: {error} </Text> </View> ); }
数据显示view
//获取到数据加载到控件上 renderData() { return ( <ScrollView style={ { paddingTop: 22, paddingLeft: 5, paddingBottom: 5, paddingRight: 5 }}> <Text style={{fontSize: 20}}>data:</Text> <ListView dataSource={this.state.dataSource} renderRow={this.renderItemView}/> </ScrollView> ); } //返回itemView renderItemView(item) { return ( <View> <Text style={{fontSize: 15, color: 'blue'}}>name: {item.name} ({item.stargazers_count} stars)</Text> <Text style={{fontSize: 13, color: 'black'}}>description: {item.description}</Text> </View> ); }
最后就是数据请求了
//rn的生命周期,初始化的时候会执行 componentDidMount() { //请求数据 this.fetchData(); } //返回当前显示的view render() { //第一次加载等待的view if (!this.state.dataSource && !this.state.error) { return this.renderLoadingView(); } else if (this.state.error) { //请求失败view return this.renderErrorView(this.state.errorInfo); } //加载数据 return this.renderData(); }
显示结果
*最后奉上全部代码
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ScrollView,
ListView
} from 'react-native';
var REQUEST_URL = 'https://api.github.com/search/repositories?q=javascript&sort=stars';
class HelloWorld extends Component {
//js组件的构造函数,js的生命周期
constructor(props) {
super(props);
this.state = {
//listView数据源
dataSource: new ListView.DataSource({
rowHasChanged: function (row1, row2) {
return row1 !== row2
},
sectionHeaderHasChanged:function (s1, s2) {
return s1 !== s2
},
}),
//网络请求状态
error: false,
errorInfo: ""
};
}
//rn的生命周期,初始化的时候会执行
componentDidMount() {
//请求数据
this.fetchData();
}
//返回当前显示的view
render() {
//第一次加载等待的view
if (!this.state.dataSource && !this.state.error) {
return this.renderLoadingView();
} else if (this.state.error) {
//请求失败view
return this.renderErrorView(this.state.errorInfo);
}
//加载数据
return this.renderData();
}
//加载等待的view
renderLoadingView() {
return (
<View style={styles.container}>
<Text>
Loading start...
</Text>
</View>
);
}
//加载失败view renderErrorView(error) { return ( <View style={styles.container}> <Text> Fail: {error} </Text> </View> ); }
//获取到数据加载到控件上
renderData() {
return (
<ScrollView style={
{
paddingTop: 22,
paddingLeft: 5,
paddingBottom: 5,
paddingRight: 5
}}>
<Text style={{fontSize: 20}}>data:</Text>
<ListView dataSource={this.state.dataSource}
renderRow={this.renderItemView}/>
</ScrollView>
);
}
//返回itemView
renderItemView(item) {
return (
<View>
<Text style={{fontSize: 15, color: 'blue'}}>name: {item.name} ({item.stargazers_count} stars)</Text>
<Text style={{fontSize: 13, color: 'black'}}>description: {item.description}</Text>
</View>
);
}
fetchData() {
//这个是js的访问网络的方法
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
//复制数据源
dataSource: this.state.dataSource.cloneWithRows(responseData.items)
});
})
.catch((error) => {
this.setState({
error: true,
errorInfo: error
})
})
.done();
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
// 注意,这里用引号括起来的'HelloWorld'必须和你init创建的项目名一致
AppRegistry.registerComponent('HelloWorld', () => HelloWorld);
相关文章推荐
- Android异步加载学习笔记之二:实现ListView中的图片数据从网络加载
- React Native 学习笔记二十(关于ListView的使用)
- React-Native傻瓜式学习笔记(一):ListView的简单使用
- ReactNative学习实例(三) 使用fetch获取网络数据
- react native listView(1) 加载网络数据简单版
- React Native学习笔记之--图片的加载
- Android网络图片加载缓存处理库的使用---第三方库学习笔记(五)
- ExtJS学习笔记2:响应事件、使用AJAX加载数据
- 使用异步任务加载网络上json数据并加载到ListView中
- react 学习笔记之AJAX数据请求和使用
- React-Native 学习笔记十七(网络请求)
- react-native ListView加载大量数据时会占用大量内存导致应用崩溃
- 5.7_Android Training 学习笔记_ 使用 Volley 执行网络数据传输
- Android异步加载学习笔记之四:利用缓存优化网络加载图片及ListView加载优化
- react native(八)加载网络数据 和chrome
- React Native 学习笔记九(ScrollView的使用)
- react native 学习笔记----网络
- Android控件使用—Listview加载网络数据、图片并跳转传值
- React Native 学习笔记五(关于样式的使用)
- Android 学习笔记之Volley(八)实现网络图片的数据加载