我遇到了我的Flatlist onEndReached方法早期调用的问题。 每次加载页面时,都会调用onEndReached方法并弄乱我的userPicture数组。 有任何想法吗?
async onEndReached() {
await this.setState({itemsToRender:
this.state.userPicture.slice(-1)[0].id})
const data = await PictureService.getRandomPicutures(
this.state.itemsToRe`enter code here`nder
);
const currentPictures = this.state.userPicture;
const newArray = currentPictures.concat(data)
await this.setState({userPicture: newArray, isFetching:false});
}
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.userPicture}
renderItem={this.renderItem}
keyExtractor={item => item.id.toString()}
onRefresh={() => this.handleRefresh()}
refreshing={this.state.isFetching}
onEndReached={this.onEndReached.bind(this)}
onEndReachedThreshold={0.5}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "white",
marginTop: 50
}
});