-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfinite-Scroll-Example.js
140 lines (134 loc) · 3.69 KB
/
Infinite-Scroll-Example.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { Component } from 'react';
import { Image, StyleSheet, Text, View, ListView, ActivityIndicator } from 'react-native';
import { Constants } from 'expo';
export default class Application extends Component {
constructor(props) {
super(props);
this.fetchMore = this._fetchMore.bind(this);
this.fetchData = this._fetchData.bind(this);
this.state = {
dataSource: null,
isLoading: true,
isLoadingMore: false,
_data: null,
_dataAfter: '',
};
}
_fetchData(callback) {
const params = this.state._dataAfter !== ''
? `&after=${this.state._dataAfter}`
: '';
//Limits fetches to 15 so there's lesser items from the get go
fetch(`https://www.reddit.com/subreddits/popular/.json?limit=15${params}`)
.then(response => response.json())
.then(callback)
.catch(error => {
console.error(error);
});
}
_fetchMore() {
this.fetchData(responseJson => {
const data = this.state._data.concat(responseJson.data.children);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data),
isLoadingMore: false,
_data: data,
_dataAfter: responseJson.data.after,
});
});
}
componentDidMount() {
//Start getting the first batch of data from reddit
this.fetchData(responseJson => {
let ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
});
const data = responseJson.data.children;
this.setState({
dataSource: ds.cloneWithRows(data),
isLoading: false,
_data: data,
_dataAfter: responseJson.data.after,
});
});
}
render() {
if (this.state.isLoading) {
return (
<View style={styles.container}>
<ActivityIndicator size="large" />
</View>
);
} else {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={rowData => {
return (
<View style={styles.listItem}>
<View style={styles.imageWrapper}>
<Image
style={{ width: 70, height: 70 }}
source={{
uri: rowData.data.icon_img === '' ||
rowData.data.icon_img === null
? 'https://via.placeholder.com/70x70.jpg'
: rowData.data.icon_img,
}}
/>
</View>
<View style={{ flex: 1 }}>
<Text style={styles.title}>
{rowData.data.display_name}
</Text>
<Text style={styles.subtitle}>
{rowData.data.public_description}
</Text>
</View>
</View>
);
}}
onEndReached={() =>
this.setState({ isLoadingMore: true }, () => this.fetchMore())}
renderFooter={() => {
return (
this.state.isLoadingMore &&
<View style={{ flex: 1, padding: 10 }}>
<ActivityIndicator size="small" />
</View>
);
}}
/>
);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
listItem: {
flex: 1,
flexDirection: 'row',
borderBottomWidth: 1,
borderBottomColor: '#d6d7da',
padding: 6,
},
imageWrapper: {
padding: 5,
},
title: {
fontSize: 20,
textAlign: 'left',
margin: 6,
},
subtitle: {
fontSize: 10,
textAlign: 'left',
margin: 6,
},
});