Created
October 20, 2020 18:05
-
-
Save riadhnet/1e184c83354f65842a08255e701c7019 to your computer and use it in GitHub Desktop.
RN FlatList component with pagination and pull-refreshing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { | |
View, | |
Text, | |
FlatList, | |
StyleSheet | |
} from 'react-native'; | |
import { ListItem ,Avatar} from 'react-native-elements'; | |
class Users extends React.Component { | |
state = { | |
seed: 1, | |
page: 1, | |
users: [], | |
isLoading: false, | |
isRefreshing: false, | |
}; | |
loadUsers = () => { | |
const { users, seed, page } = this.state; | |
this.setState({ isLoading: true }); | |
fetch(`https://randomuser.me/api/?seed=${seed}&page=${page}&results=20`) | |
.then(res => res.json()) | |
.then(res => { | |
this.setState({ | |
users: page === 1 ? res.results : [...users, ...res.results], | |
isRefreshing: false, | |
}); | |
}) | |
.catch(err => { | |
console.error(err); | |
}); | |
}; | |
handleRefresh = () => { | |
this.setState({ | |
seed: this.state.seed + 1, | |
isRefreshing: true, | |
}, () => { | |
this.loadUsers(); | |
}); | |
}; | |
handleLoadMore = () => { | |
this.setState({ | |
page: this.state.page + 1 | |
}, () => { | |
this.loadUsers(); | |
}); | |
}; | |
componentDidMount() { | |
this.loadUsers(); | |
}; | |
render() { | |
const { users, isRefreshing } = this.state; | |
return ( | |
<View style={s.scene}> | |
{ | |
users && | |
<FlatList | |
data={users} | |
renderItem={({ item }) => ( | |
<ListItem > | |
<Avatar source={{ uri: item.picture.thumbnail}} /> | |
<ListItem.Content> | |
<ListItem.Title>{item.name.first}</ListItem.Title> | |
<ListItem.Subtitle>{item.email}</ListItem.Subtitle> | |
</ListItem.Content> | |
</ListItem> | |
)} | |
keyExtractor={i => i.email} | |
refreshing={isRefreshing} | |
onRefresh={this.handleRefresh} | |
onEndReached={this.handleLoadMore} | |
onEndThreshold={0} | |
/> | |
} | |
</View> | |
) | |
} | |
} | |
const s = StyleSheet.create({ | |
scene: { | |
flex: 1, | |
paddingTop: 25, | |
}, | |
user: { | |
width: '100%', | |
backgroundColor: '#333', | |
marginBottom: 10, | |
paddingLeft: 25, | |
}, | |
userName: { | |
fontSize: 17, | |
paddingVertical: 20, | |
color: '#fff' | |
} | |
}); | |
export default Users; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment