react-infinite-scroll-component 用数组加载更多

艾比熊

我正在尝试使用我的数组数据进行无限滚动。我看到的大多数示例都使用 API 调用并将其pages包含在其中,但是我无法仅使用数组和值来设置每个滚动加载的对象数量。这是我的代码。

import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import InfiniteScroll from 'react-infinite-scroll-component'

const Scroll = ({ tweets }) => {
  const [allTweets, setAllTweets] = useState(tweets)
  const [hasMore, setHasmore] = useState(true)
  const [lastPosition, setLastPosition] = useState(0)
  const perPage = 4

  const loadProducts = () => {
    setTimeout(() => {
      setAllTweets(...allTweets, tweets.slice(lastPosition, lastPosition + perPage))
    }, 4000)

    setLastPosition(lastPosition + perPage)
  }

  useEffect(() => {
    loadProducts()
  }, [allTweets])

  return (
    <InfiniteScroll
      dataLength={allTweets.length}
      next={loadProducts}
      hasMore={hasMore}
      endMessage={
        <p style={{ textAlign: 'center' }}>
          <b>Yay! You have seen it all</b>
        </p>
      }
      loader={<h4>Loading...</h4>}
    >
      <div className="flex flex-col">
        {allTweets &&
          allTweets.slice(lastPosition, lastPosition + perPage).map((value, index) => {
            return <div className="py-10 px-5">{value.body}</div>
          })}
      </div>
    </InfiniteScroll>
  )
}

export default Scroll
杰克·本森

我做了一个我认为你正在尝试做的半功能示例请注意,此插件似乎只有在您可以滚动时才有效如果您的内容不够高而无法强制滚动,那么新内容将永远无法加载:

应用程序.js

import React from "react";
import Scroll from "./Scroll";

const App = () => {
  const initialTweets = [
    { body: "Body 1" },
    { body: "Body 2" },
    { body: "Body 3" },
    { body: "Body 4" },
    { body: "Body 5" },
    { body: "Body 6" },
    { body: "Body 7" },
    { body: "Body 8" },
    { body: "Body 9" },
    { body: "Body 10" }
  ];

  return (
    <div>
      <h1>demo: react-infinite-scroll-component</h1>
      <hr />
      <Scroll tweets={initialTweets} />
    </div>
  );
};

export default App;

滚动.jsx

import React, { useCallback, useState } from "react";
import InfiniteScroll from "react-infinite-scroll-component";

const Scroll = ({ tweets }) => {
  const [allTweets, setAllTweets] = useState(tweets);
  const [hasMore, setHasmore] = useState(true);
  const [lastPosition, setLastPosition] = useState(0);
  const perPage = 4;

  const loadProducts = useCallback(() => {
    setTimeout(() => {
      setAllTweets((prev) => [...prev, ...prev]);
    }, 2000);

    setLastPosition(lastPosition + perPage);
  }, []);

  console.log(allTweets);

  return (
    <InfiniteScroll
      dataLength={allTweets.length}
      next={loadProducts}
      hasMore={hasMore}
      loader={<h4>Loading...</h4>}
    >
      <div className="flex flex-col">
        {allTweets.map((value, index) => (
          <div key={index} className="py-10 px-5">
            {value.body}
          </div>
        ))}
      </div>
    </InfiniteScroll>
  );
};

export default Scroll;

您需要调整事情以完全按照您的意愿行事,但这应该为您指明正确的方向。

注意:要在 codeandbox 中查看这项工作,请缩小演示窗口,以便您可以滚动或将更多对象添加到initialTweets数组中。此外,您可以在项目主页上找到许多示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用react-infinite-scroll-component的Firestore限制仅以给定限制滚动

在react-infinite-scroll-component中的每个新搜索上替换获取的搜索结果

如何在Material-UI对话框模态中使用react-infinite-scroll-component?

React Infinite Scroll 超出最大更新深度

如何在React Native中使用Infinite Scroll?

React Redux + Infinite Scroll =重新渲染整个列表?

当Infinite Scroll包含在顶部带有HTML标记的另一个文件中时,不会向下滚动加载更多帖子

Chrome窗口无法从Infinite Scroll加载内容,除非该窗口(标签)处于焦点位置

使用infinite-scroll.js库在内容加载后执行回调

使用Wordpress Infinite Scroll加载产品后,再次重新初始化Woocommerce脚本

用Jest测试React Component函数

React Native Component 将被弃用

Infinite-scroll listview.builder - 扩展或不扩展...以及更多提供程序值未更新以及如何修复“RenderFlex 溢出”

React Component或React PureComponent

Scrapy for eCommerce with Selenium Infinite Scroll,帮助返回值

React native,Redux:获取更多/加载更多

扩展React.Component与Component

在React中加载更多按钮

React Component无法在stateChange上正确重新加载

带有React Component的SharePoint框架加载图像

React Component未加载Webpack和Babel Beginner的级别

如何在React Native中传递View / Component的数组?

React Component的状态,响应中数组中对象的错误顺序

如何在React Component中打印数组

React Component和CSSTransitionGroup

React Component API架构

React Component编辑数据

在React中将引用从Component传递到Component

扩展Component <Props>与扩展React.Component