简体   繁体   English

ReactNative Flatlist onEndReached 不工作

[英]ReactNative Flatlist onEndReached not working

Im trying to call a function on onEndReached of a FlatList but it's not working.我试图在onEndReached的 onEndReached 上调用 function,但它不起作用。

I'm calling the this.state.pageNo at the end and it's not updating.我最后调用了this.state.pageNo并且它没有更新。 I want to use this logic later in infinite scroll but not able to get it working now.我想稍后在无限滚动中使用这个逻辑,但现在无法让它工作。

 import React, { Component } from "react";
 import {
  View,
  Text,
  StyleSheet,
  Button,
  TouchableOpacity,
  FlatList,
  Alert
  } from "react-native";

 class InfiniteScrollRedux extends Component {
   constructor(props) {
      super(props);
      this.state = {
        loading: false,
        pageNo: 1,
        data: [
            { id: 1, name: "Name01" },
            { id: 2, name: "Name02" },
            { id: 3, name: "Name03" },
            { id: 4, name: "Name04" },
            { id: 5, name: "Name05" },
            { id: 6, name: "Name06" }
        ]
    };
}

myFunction = () => {
    this.setState({ pageNo: this.state.pageNo++ });
};

render() {
    return (
        <View>
            <FlatList
                data={this.state.data}
                renderItem={({ item }) => (
                    <View style={mystyle.mainCard}>
                        <Text style={mystyle.titleText}> {item.id} </Text>
                        <View style={{ marginTop: 200 }} />
                        <Text style={mystyle.nameText}> {item.name} </Text>
                    </View>
                )}
                keyExtractor={item => item.id}
                onEndReached={this.myFunction}
                onEndThreshold={0}
            />
            <Text style={{ margin: 20, padding: 20, fontSize: 20 }}>
                {this.state.pageNo}
            </Text>
        </View>
      );
   }
}

const mystyle = StyleSheet.create({
 mainCard: {
    backgroundColor: "#2F4F4F",
    marginBottom: 1,
    padding: 5
},
titleText: {
    fontSize: 20,
    color: "#F0FFFF"
},
nameText: {
    fontSize: 14,
    color: "#F0FFFF"
 }
 });

 export default InfiniteScrollRedux;

In my case the problem was with nativebase <Content> .就我而言,问题出在 nativebase <Content> It was creating problems when <FlatList> was used inside it.在其中使用<FlatList>时会产生问题。 Solution :解决方案:

<Content
  style={{flex: 1}}
  contentContainerStyle={{flex: 1}} // important!
>

Source : https://github.com/GeekyAnts/NativeBase/issues/1736来源: https : //github.com/GeekyAnts/NativeBase/issues/1736

There is an issue for that here: https://github.com/facebook/react-native/issues/14312 .这里有一个问题: https : //github.com/facebook/react-native/issues/14312 Looks like a lot of people are experiencing the same.看来很多人都在经历同样的事情。 There is a suggestion to change the onEndReachedThreshold to value bigger that 0, for example: 0.3.建议将onEndReachedThreshold更改为大于 0 的值,例如:0.3。

您在 FlatList 中查找的属性是onEndReachedThreshold而不是 onEndThreshold。

First of all, you should make sure that your onEndReached listens to your onMomentumScrollBegin and onMomentumScrollEnd props of FlatList .首先,您应该确保您的onEndReached监听您的onMomentumScrollBeginonMomentumScrollEnd道具的FlatList Another important thing is distanceFromEnd param of onEndReached prop of FaltList .另一个重要的事情是distanceFromEnd的PARAM onEndReached的道具FaltList So you can use it as follows:因此,您可以按如下方式使用它:

          onMomentumScrollBegin={() => this.setState({ scrollBegin: true })}
          onMomentumScrollEnd={() => this.setState({ scrollBegin: false })}
          onEndReached={({ distanceFromEnd }) =>
            distanceFromEnd<=0.5&&
            this.state.scrollBegin &&
            this.onEndReached()
          }

And then you can define your onEndReached function, which will work as needed.然后你可以定义你的onEndReached函数,它会根据需要工作。

Hope this will help someone to save time on this.希望这会帮助某人节省时间。

Got stuck with this issue for a long time.被这个问题困扰了很久。 I think React is having confusions in calculating the screen height vs the container height.我认为React在计算屏幕高度与容器高度时存在混淆。 So the best thing we can do is to increase the onEndReachedThreshold value to something higher than 0 to some 0.2, 0.4 etc.因此,我们可以做的最好的事情是增加onEndReachedThreshold价值的东西高于0一些0.2, 0.4等。

for improving onEndReached performance I suggest to provide exact height to contentContainerStyle for example:为了提高 onEndReached 的性能,我建议为 contentContainerStyle 提供准确的高度,例如:

contentContainerStyle={{ height : items.length * itemHeight }}

Just sharing one more reason why it wasn't working on my side.只是再分享一个为什么它对我不起作用的原因。 My FlatList was inside a ScrollView which was not letting the FlatList know that it has reached end.我的 FlatList 在 ScrollView 里面,它没有让 FlatList 知道它已经结束了。 I moved my FlatList out of the ScrollView and it started to work again!我将 FlatList 移出 ScrollView,它又开始工作了!

设置onEndReachedThreshold为我解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM