javascript - React JS - 在数组末尾停止无限滚动

标签 javascript reactjs flickr

我有一个简单的 React 应用程序,我可以在其中获取 Flickr 公共(public)源。因此,我可以滚动到页面末尾,新内容将显示。因此,我想滚动,直到没有其他新内容,并且应用程序停止尝试加载更多内容,因为它已到达列表的最后一项,如果我尝试滚动,则不会发生这种情况(您可以在加载消息)。我怎样才能解决这个问题?

检查下面的代码:

import React, { Component } from "react";
import ReactDOM from "react-dom";
import $ from "jquery";

import PhotoListItem from "./photoListItem";

import "./photoApp.css";

export default class PhotoApp extends Component {
  constructor(props) {
    super(props);

    this.state = {
      photoList: [],
      searchTerm: "cyanotype",
      items: 8,
      loadingState: false,
      loadingMessage: ""
    };
  }

  componentDidMount() {
    this.getPhotoList();
    this.onInfiniteScroll();
  }

  /* get data from Flickr public feed */
  getPhotoList = () => {
    const flickrApiPoint =
      "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?&tags=" +
      this.state.searchTerm;

    try {
      $.ajax({
        url: flickrApiPoint,
        dataType: "jsonp",
        data: { format: "json" },
        success: function(data) {
          this.setState({ photoList: data.items });
        }.bind(this)
      });
    } catch (err) {
      console.log(err);
    }
  };

  /* code for infinite scroll */
  onInfiniteScroll = () => {
    this.refs.iScroll.addEventListener("scroll", () => {
      if (
        this.refs.iScroll.scrollTop + this.refs.iScroll.clientHeight >=
        this.refs.iScroll.scrollHeight - 20
      ) {
        this.loadMoreItems();
      }
    });
  };

  /* code for infinite scroll */
  loadMoreItems = () => {
    if (this.state.loadingState) {
      return;
    }

    this.setState({
      loadingState: true,
      loadingMessage: "Loading photos..."
    });
    setTimeout(() => {
      this.setState(prevState => ({
        items: prevState.items + 8,
        loadingState: false,
        loadingMessage: "No more photos to show."
      }));
    }, 1000);
  };

  render() {
    console.log(this.state.photoList)
    return (
      <div className="appContainer" ref="iScroll">
        <div className="appHeader">
          <h1 className="headerTitle">
            Welcome to Flickr Alternative Photography Feed!
          </h1>
        </div>

        <div className="gridContainer">
          {this.state.photoList
            .slice(0, this.state.items)
            .map((photo, index) => {
              const author = photo.author.split(/"/)[1];
              const authorLink = photo.description.split(/"/)[1];
              const description = photo.description.split(/"/)[13];
              return (
                <PhotoListItem
                  key={index}
                  url={photo.media.m}
                  photoLink={photo.link}
                  title={photo.title}
                  author={author}
                  authorLink={authorLink}
                  description={description}
                  tags={photo.tags}
                />
              );
            })}
        </div>

        <React.Fragment>
          {this.state.loadingState ? (
            <p className="loading">{this.state.loadingMessage}</p>
          ) : (
            <p className="loading">{this.state.loadingMessage}</p>
          )}
        </React.Fragment>
      </div>
    );
  }
}

LIVE EXAMPLE HERE

谢谢!

最佳答案

您可以检查您加载的项目是否超出了ajax请求中的项目


  /* code for infinite scroll */
  loadMoreItems = () => {
    // hasMore = data.items.length (you may want to rename this more appropriately)
    if (this.state.loadingState || (this.state.items > this.state.hasMore)) {
      // Do not load if there's no more items
      return;
    }
    ...

关于javascript - React JS - 在数组末尾停止无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57663110/

相关文章:

javascript - Selenium IDE : How to verify <TD> Contents when it contains a mix of tags and <BR> breaks

django - React 和 Nginx 在使用 axios 时弄乱了 url,不正确的 API 调用

javascript - React的生命周期方法是自动绑定(bind)的吗?如果不是,我们应该用 .bind(this) 绑定(bind)它们吗?

reactjs - 通过webpack运行babel-loader后,组件中未定义 'this'

javascript - PHP 表单不向 MySQL 发送新数据

javascript - VUE.JS 将数据传递给 D3 - 做对了吗?

javascript - 为什么 Array.prototype.sort() 在 Chrome 开发工具中不起作用?

android - 在 flickr 中上传图片显示错误

jquery - 为什么带有 jQ​​uery 代码的 Flickr API 不起作用?

android - Flickr-Android API : Oauth is not redirecting to 'grant-access' page