react-native – FlatList在呈现时调用`onEndReached`

前端之家收集整理的这篇文章主要介绍了react-native – FlatList在呈现时调用`onEndReached`前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的简单类别列表页面的render()函数.

最近我为我的FlatList View添加分页,所以当用户滚动到底部时,onEndReached在某个点(从底部开始的onEndReachedThreshold值长度)被调用,它将获取下一个类别并连接类别props.

但我的问题是在调用render()时调用onEndReached.换句话说,FlatList的onEndReached在它到达底部之前被触发.

我是否为onEndReachedThreshold设置了错误的值?你看到有什么问题吗?

  1. return (
  2. <View style={{ flex:1 }}>
  3. <FlatList
  4. data={this.props.categories}
  5. renderItem={this._renderItem}
  6. keyExtractor={this._keyExtractor}
  7. numColumns={2}
  8. style={{flex: 1,flexDirection: 'row'}}
  9. contentContainerStyle={{justifyContent: 'center'}}
  10. refreshControl={
  11. <RefreshControl
  12. refreshing = {this.state.refreshing}
  13. onRefresh = {()=>this._onRefresh()}
  14. />
  15. }
  16. // curent value for debug is 0.5
  17. onEndReachedThreshold={0.5} // Tried 0,0.01,0.1,0.7,50,100,700
  18.  
  19. onEndReached = {({distanceFromEnd})=>{ // problem
  20. console.log(distanceFromEnd) // 607,878
  21. console.log('reached'); // once,and if I scroll about 14% of the screen,//it prints reached AGAIN.
  22. this._onEndReachedThreshold()
  23. }}
  24. />
  25. </View>
  26. )

更新我在这里获取this.props.categories数据

  1. componentWillMount() {
  2. if(this.props.token) {
  3. this.props.loadCategoryAll(this.props.token);
  4. }
  5. }
尝试在FlatList上实现onMomentumScrollBegin:
  1. constructor(props) {
  2. super(props);
  3. this.onEndReachedCalledDuringMomentum = true;
  4. }

  1. <FlatList
  2. ...
  3. onEndReached={this.onEndReached.bind(this)}
  4. onEndReachedThreshold={0.5}
  5. onMomentumScrollBegin={() => { this.onEndReachedCalledDuringMomentum = false; }}
  6. />

修改你的onEndReached

  1. onEndReached = ({ distanceFromEnd }) => {
  2. if(!this.onEndReachedCalledDuringMomentum){
  3. this.fetchData();
  4. this.onEndReachedCalledDuringMomentum = true;
  5. }
  6. }

猜你在找的React相关文章