如何使用REACT/TYPESCRIPT创建无限卷轴? 我想首先显示一个10个帖子的列表,然后当用户查看第10个帖子时,屏幕上应该加载另一个阶段的10. 请帮忙做一个代码演示.

这是我目前所实现的,目前正在显示所有帖子

import PostStyles from "../../../components/ui/createPost/_createpost.module.scss"
import CreatePost from "../createPost/createPost"
import Post from "../post/post"
import Modal from "../createPost/Modal"
import { postType } from "../../../util/types"


type mainSectionType = {
  posts:postType[]
}

type postType = {
    _id: string;
    creator: {
      id: string,
      name: string,
      profilePicture: string
    },
    createdAt: string,
    photos: string[],
    post_description: string,
    time_posted: string,
  }

const MainSection = ({posts}:mainSectionType) => {


  return (
    <>
      <Modal />
      <div className={PostStyles.post__create_postContainer}>
        <CreatePost />
      </div>
      <div className={PostStyles.post__posts_container}>
         {
          posts.map((post, index) => {
            return <Post
              key={index}
              post_desc={post.post_description}
              postID={post._id}
              username={post.creator.name}
              timeposted={post.time_posted}
              userImg={post.creator.profilePicture}
              photos={post.photos}
            />
          })
        } 
      </div>

    </>
  )
}

export default MainSection

推荐答案

您可以使用此程序包进行无限滚动->;

包名:‘REACTION-INFINITE-SCROLL-COMPOMENT’

示例:

州:

   const [startIndex, setStartIndex] = useState(0);
      const [slicedData, setSlicedData] = useState([] as interface type name here);
   const [hasMore, setHasMore] = useState(true);

接口响应:

   setSlicedData(res.slice(0, 10));

HandleNextData函数:

const handleNextData = () => {
        /*endIndex find min index like startIndex start from 0 then + 100 and 
data.length from that minimum number will be endIndex. */
    
const endIndex = Math.min(startIndex + 100, allMaterialList.length);
        
        /* In nextBatch where startIndex and endIndex represent the index of items 
                in that allMaterialList . method returns a shallow copy of a portion of an 
                            array into a new array object */


        const nextBatch = allMaterialList.slice(startIndex, endIndex);
        setSlicedData((prevData) => [...prevData, ...nextBatch] as interface name here);

        setHasMore(endIndex < allMaterialList.length);

        setStartIndex(endIndex);
};

组件:

<InfiniteScroll
    dataLength={your store data state name here.length}                     
    next={handleNextData}
    hasMore={hasMore}
    loader={<h4>Loading more 2 items...</h4>}
    scrollableTarget='scrollableDiv'
>
    {Children}
</InfiniteScroll>

Javascript相关问答推荐

我的glb文件没有加载到我的three.js文件中

如何修复内容安全策略指令脚本-SRC自身错误?

如何使用Echart 5.5.0创建箱形图

为什么!逗号和空格不会作为输出返回,如果它在参数上?

如何将Cookie从服务器发送到用户浏览器

为什么我的导航条打开状态没有在窗口addeventlistener(Reaction Js)中更新?

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

覆盖TypeScrip中的Lit-Element子类的属性类型

以编程方式聚焦的链接将被聚焦,但样式不适用

如何确保预订系统跨不同时区的日期时间处理一致?

我不知道如何纠正这一点.

有没有办法通过使用不同数组中的值进行排序

如何在Jest中模拟函数

在HTML5画布上下文中使用putImageData时,重载解析失败

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

如何正确地在ComponentWillUnmount中卸载状态以避免内存泄漏?

Plotly.js栏为x轴栏添加辅助文本

限制数组中每个元素的长度,

如何在不将整个文件加载到内存的情况下,在Node.js中实现Unix粘贴命令?

浮动标签效果移除时,所需的也被移除