我已经在我的列表中添加了onEndReached,它可以正常工作。 现在,如果我完成了要提取的项目,就必须停止它。 如果我在没有足够的元素获取时调用该函数,则会出现错误:“未定义不是对象(正在评估item.title.rendered)”
这是我的单位列表:
<FlatList
ItemSeparatorComponent={this.FlatListItemSeparator}
data={this.state.dataSource}
renderItem={({ item }) => (
//on touch --> open article (call _onTextPress)
<TouchableWithoutFeedback onPress={this._onTextPress.bind(this, item.id)}>
<View style={{flexDirection:'row', justifyContent: 'space-between'}}>
<View style={styles.titoletti}>
<HTML html={'<p style="color:#fd3a18; font-size:17px;"><strong>'+item.title.rendered.substring(0,70)+'...</strong></p>\n'}/>
<Text style={styles.date}>{item.date.substring(0, 10)}</Text>
</View>
<View style={{flexDirection:'column', justifyContent:'space-between'}}>
<View></View>
<Icon
name="ios-arrow-forward"
style={styles.rightIcon}/>
<View></View>
</View>
</View>
</TouchableWithoutFeedback>
)}
keyExtractor={({ id }, index) => String(id)}
onEndReached={this.handleLoadMore}
onEndReachedThreshold ={0.01}
ListFooterComponent={this.renderFooter}
/>
这是onEndReachde调用的函数:
handleLoadMore = () => {
this.setState({
page: this.state.page +1 },
this.getData
)
}
componentDidMount() {
this.getData();
}
getData = async() => {
const { navigation } = this.props;
const id = String(navigation.getParam('id'));
this.setState({
title: String(navigation.getParam('title')),
isLoading: true,
})
return fetch('https://www.rallyssimo.it/wp-json/wp/v2/posts?per_page=10&categories='+id+'&page='+this.state.page)
.then(response => response.json())
.then(responseJson => {
this.setState(
{
dataSource: this.state.dataSource.concat(responseJson),
isLoading: false,
},console.log(this.state.dataSource.lenght),
function() {}
);
})
.catch(error => {
console.error(error);
});
}