-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRNLoadMore.js
More file actions
93 lines (86 loc) · 2.13 KB
/
Copy pathRNLoadMore.js
File metadata and controls
93 lines (86 loc) · 2.13 KB
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
import React, { Component } from 'react';
import { StyleSheet, FlatList, View, Text, Image, ActivityIndicator } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
data: [],
page: 1,
isLoading: false
}
}
componentDidMount() {
this.setState({isLoading: true}, this.getData)
}
getData = async () => {
const apiURL = "https://jsonplaceholder.typicode.com/photos?_limit=10&_page="
+ this.state.page;
// ?_limit=10&_page=
fetch(apiURL).then((res) => res.json())
.then((resJson) => {
this.setState({
data: this.state.data.concat(resJson),
// function "concat" used to join two or more arrays
isLoading: false
})
})
}
renderRow = ({item}) => {
return (
<View style={styles.itemRow}>
<Image source={{uri: item.url}} style={styles.itemImage} />
<Text style={styles.itemText}>{item.title}</Text>
<Text style={styles.itemText}>{item.id}</Text>
</View>
)
}
renderFooter = () => {
// ActivityIndicator Displays a circular loading indicator.
return (
this.state.isLoading ?
<View style={styles.loader}>
<ActivityIndicator size="large"/>
</View> : null
)
}
handleLoadMore = () => {
this.setState({ page: this.state.page + 1, isLoading: true }, this.getData)
}
render() {
return (
<FlatList
style={styles.container}
data={this.state.data}
renderItem={this.renderRow}
keyExtractor={(item, index) => index.toString()}
onEndReached={this.handleLoadMore}
onEndReachedThreshold={0}
ListFooterComponent={this.renderFooter}
/>
)
}
}
const styles = StyleSheet.create({
container: {
marginTop: 20,
backgroundColor: '#f5fcff'
},
itemRow: {
borderBottomColor: '#ccc',
marginBottom: 10,
borderBottomWidth: 1
},
itemImage: {
width: '100%',
height: 200,
resizeMode: 'cover'
},
itemText: {
fontSize: 16,
padding: 5
},
loader: {
marginTop: 10,
alignItems: 'center'
}
})