//用于绘制瀑布流视图组件,
import Masonry from 'react-masonry-component';

const masonryOptions = {
  gutter: 10,
};

//滚动到底部进行翻页
const handleOnScroll = (e: any) => {
    if (e.target) {
      const contentScrollTop = e.target.scrollTop; //滚动条距离顶部
      const clientHeight = e.target.clientHeight; //可视区域
      const scrollHeight = e.target.scrollHeight; //滚动条内容的总高度
      if (pageNum >= total) {
        return;
      }
      if (contentScrollTop + clientHeight >= scrollHeight) {
        setPageNum(pageNum + 1);
        //调用接口,将获取的数据拼接在一起
        addList(pageNum + 1);
      }
    }
  };
<div style={{ height: '100%', overflow: 'auto' }} onScrollCapture={handleOnScroll}>
        <Masonry
          className={'my-gallery-class'} // default ''
          options={masonryOptions} // default {}
          disableImagesLoaded={false} // default false
          updateOnEachImageLoad={false} // default false and works only if disableImagesLoaded is false
        >
          {dataList.map((item) => (
            <div style={{ marginBottom: '10px' }} key={item.id}>
            //每个item渲染
              <IssueTopicItem
                data={item}
                reload={() => {
                //重新加载,数据重置
                  queryPageList(1);
                  setPageNum(1);
                }}
              />
            </div>
          ))}
        </Masonry>
        <Spin spinning={loading} style={{ width: '100%', margin: 'auto' }} />
      </div>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐