ios - FlatList onEndReached 被多次调用

标签 ios react-native reactive-programming react-native-flatlist

<分区>

我正在制作一个 React Native 项目,用户可以在其中使用 Flickr API 搜索图像,其他一切工作正常,但我在实现分页时遇到了问题。我已经使用 FlatList 的 onEndReached 来检测用户何时滚动到列表的末尾,但问题是 onEndReached 被多次调用(包括第一次渲染期间的一次) .如前所述,我什至禁用了弹跳 here但它仍然被调用不止一次

 export default class BrowserHome extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isLoading: false,
      tagParam: "cat",
      pageNum: -1,
      data: [],
      photosObj: ""
    };
  }

  componentDidMount() {
    this.setState({
      isLoading: true
    });
    try {
      this.makeRequest();
    } catch {
      console.log("error has occurred");
    }
  }

  makeRequest = () => {
    const { tagParam, pageNum } = this.state;
    let url = `https://api.flickr.com/services/rest/? 
               method=flickr.photos.search
               &api_key=${apiKey}&format=json&tags=${tagParam}
               &per_page=30&page=${pageNum + 1}&nojsoncallback=1`;
    fetch(url, {
      method: "GET"
    })
      .then(response => response.json())
      .then(responseJSON => {
        this.setState({
          data: this.state.data.concat(responseJSON.photos.photo),
          isLoading: false,
          pageNum: responseJSON.photos.page
        });
      })
      .catch(error => {
        console.log(error);
        this.setState({ isLoading: false });
        throw error;
      });
  };

  render() {
    if (this.state.isLoading) {
      return <ActivityIndicator animating={true} size="large" />;
    }

    return (
      <View
        style={{
          flex: 1,
          height: 200,
          justifyContent: "flex-start",
          width: screenSize.width,
          backgroundColor: "black"
        }}
      >
        <Text>This is browserhome</Text>
        <FlatList
          style={{
            width: screenSize.width
          }}
          numColumns={3}
          data={this.state.data}
          keyExtractor={item => item.id}
          bounces={false}
          onEndReachedThreshold={1}
          onEndReached={({ distanceFromEnd }) => {
            this.loadMoreItem();
            alert("end reached call");
          }}
          renderItem={({ item, index }) => (
            <>
              <ImageTile imageURL={this.createImageURL(item)} />
            //  <Text style={{ color: "white" }}>
             //   {index}
             //   {console.log(index)}
             // </Text>
            </>
          )}
        />
      </View>
    );
  }

  createImageURL(item) {
    let server = item.server,
      id = item.id,
      secret = item.secret;
    let urlString = `https://farm${
      item.farm
    }.staticflickr.com/${server}/${id}_${secret}_s.jpg`;
    return urlString;
  }

  loadMoreItem() {
    this.makeRequest();
  }
}

最佳答案

这个解决方案对我有用。在FlatList组件中添加onMomentumScrollBegin并修改onEndReached

<FlatList
style = { ...}
data = {data}
initialNumToRender = {10}
onEndReachedThreshold = {0.1}
onMomentumScrollBegin = {() => {this.onEndReachedCalledDuringMomentum = false;}}
onEndReached = {() => {
    if (!this.onEndReachedCalledDuringMomentum) {
      this.retrieveMore();    // LOAD MORE DATA
      this.onEndReachedCalledDuringMomentum = true;
    }
  }
}
/>

关于ios - FlatList onEndReached 被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408470/

相关文章:

ios - 为什么我的 NSDateFormatter 返回 null?

ios - 如何构建一个 react native 应用程序,以便它在 ipad 上作为 ipad 应用程序运行?

android - React Native 致命崩溃

objective-c - 如何使用 ReactiveCocoa 简化我的嵌套 for 循环?

java - 使用 RxAndroid 生成树排序结构

ios - UISearchBar 在添加到 iOS 7.1 上的 UITableViewCell 时更改其 super View

ios - 如何根据从纬度和经度计算出的距离对数组进行排序。

ios - 为什么我不能在 Alamofire 4.0 中使用 SessionManager 发出请求?

java - 在 React Native Android 应用程序中通过 crashlytics 的堆栈跟踪找到崩溃是真的吗?

Haskell:FRP react 性 Parsec?