-
Notifications
You must be signed in to change notification settings - Fork 1
/
5multiRowWithFetch.js
107 lines (94 loc) · 2.31 KB
/
5multiRowWithFetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
/*
1. 数据数组删掉,加入http的数据接口url(REQUEST_URL)
2. 创建fetchData方法,进行http请求拉数据的操作
3. 添加了componentDidMount生命周期函数,在控件渲染完毕后,去调用fetchData方法
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
Image,
ListView,
} = React;
// multiRowWithFetch:这里是线上数据接口模拟
var REQUEST_URL = 'http://10.168.0.151/caichao/qinglist.js';
// multiRowWithFetch:添加了fetchData方法;
var reactdemo = React.createClass({
getInitialState: function() {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
})
};
},
// multiRowWithFetchUE:控件加载完成,再获取数据
componentDidMount: function() {
this.fetchData();
},
// multiRowWithFetch:添加了fetchData方法;
fetchData: function() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData)
});
})
.done();
},
render: function() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderQing}
style={styles.listView}
/>
);
},
renderQing: function(item) {
return (
<View style={styles.row}>
<Image
style={styles.img}
source={{uri:item.imgsrc}} />
<View style={styles.intro}>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.desc}>{item.desc}</Text>
</View>
</View>
)
}
});
// multiRowWithArray:为ListView加入新追加的ListView样式styles.listView
var styles = StyleSheet.create({
row: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
flexDirection: 'row',
},
img: {
width: 50,
height: 50,
marginRight: 10
},
intro: {
flex: 1,
},
title: {
fontSize: 24,
},
listView: {
paddingTop: 20, // 给系统上面信息条留出空间
backgroundColor: '#F5FCFF',
},
});
AppRegistry.registerComponent('reactdemo', () => reactdemo);