'use strict';
import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
Image,
ToastAndroid,
} from 'react-native';
var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';
class AwesomeProject extends Component {
//返回当前显示的view
render() {
// ToastAndroid.show(JSON.stringify(this.state.movies),ToastAndroid.SHORT)
//由于刚开始的的时候设置movies的职位空,所以第一次会加载等待的view
if (!this.state.movies) {
return this.renderLoadingView();
}
//只获取第一个数组的数据
var movie = this.state.movies[0];
return this.renderMovie(movie);
}
//加载等待的view
renderLoadingView() {
return (
<View style={styles.container}>
<Text>
Loading movies...
</Text>
</View>
);
}
//获取到数据加载到控件上
renderMovie(movie) {
return (
<View style={styles.container}>
<Image
source={{uri: movie.posters.thumbnail}}
style={styles.thumbnail}
/>
<View style={styles.rightContainer}>
<Text style={styles.title}>{movie.title}</Text>
<Text style={styles.year}>{movie.year}</Text>
</View>
</View>
);
}
//js组件的构造函数,js的生命周期
constructor(props) {
super(props);
//state内部维护的一个状态,我们刚开始进来的为空,来加载空的view
this.state = {
movies: null,
};
}
//rn的生命周期,初始化的时候会执行
componentDidMount() {
//去拉取电影的接口的数据
this.fetchData();
}
fetchData() {
//这个是js的访问网络的方法
fetch(REQUEST_URL)
//ES6的写法左边代表输入的参数右边是逻辑处理和返回结果
.then((response) => response.json())
.then((responseData) => {
this.setState({
movies: responseData.movies,
});
})
.done();
}
}
const styles = StyleSheet.create({
title: {
fontSize: 20,
marginBottom: 8,
textAlign: 'center',
},
year: {
textAlign: 'center',
},
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
rightContainer: {
flex: 1,
},
thumbnail: {
width: 53,
height: 81,
},
});
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
React Native学习笔记加载网络上数据显示出来
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 使用ListView加载网络数据 本文采用fecth来进行网络请求的 创建一个构造器 创建网络请求 创建Loadi...
- React-Native 学习笔记 - 使用FlastList加载网络数据 声明变量 网络请求方法 加载等待的Vi...