【问题标题】:React InfiniteScroll in a scrollable component on the page在页面上的可滚动组件中反应 InfiniteScroll
【发布时间】:2020-04-28 13:59:02
【问题描述】:

我正在尝试在具有固定高度的 div 中构建一个无限滚动并附加一个滚动条,所以我的目标是让窗口不移动,而是让其中的组件具有滚动和要添加的项目无穷无尽。

这是我目前所拥有的:

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

const style = {
  height: 18,
  border: "1px solid green",
  margin: 6,
  padding: 8
};

const DoseListCardBody = () => {
  const [items, setItems] = useState(Array.from({ length: 20 }));

  const fetchMoreData = () => {
    setItems(items.concat(Array.from({ length: 10 })));
  };

  return (
    <div style={{ height: "100%", overflowY: "scroll" }}>
      <InfiniteScroll
        dataLength={items.length}
        next={fetchMoreData}
        hasMore={items.length < 200}
        loader={<h4>Loading...</h4>}
      >
        {items.map((i, index) => (
          <div style={style} key={index}>
            div - #{index}
          </div>
        ))}
      </InfiniteScroll>
    </div>
  );
};

ReactDOM.render(
  <div style={{ height: "35rem", background: "black" }}>
    <div style={{ height: "30rem", background: "white" }}>
      <DoseListCardBody />
    </div>
  </div>,
  document.getElementById("root")
);

如果我改变一切都很好

ReactDOM.render(
  <div style={{ height: "35rem", background: "black" }}>
    <div style={{ height: "30rem", background: "white" }}>
      <DoseListCardBody />
    </div>
  </div>,
  document.getElementById("root")
);

ReactDOM.render(
  <DoseListCardBody />,
  document.getElementById("root")
);

我认为这是因为它使用的是窗口的滚动而不是组件。

如何让 InfiniteScroll 使用父组件或带有我指定的滚动的组件。

我很抱歉用了不好的术语,我通常不开发网页。

【问题讨论】:

    标签: reactjs infinite-scroll


    【解决方案1】:

    好的!

    必须在 InfiniteScroll 中使用 scrollableTarget 作为道具,并指定具有滚动条的组件的 ID。

    示例:

    const DoseListCardBody = () => {
      const [items, setItems] = useState(Array.from({ length: 20 }));
    
      const fetchMoreData = () => {
        setItems(items.concat(Array.from({ length: 10 })));
      };
    
      return (
        <div id="scrollableDiv" style={{ height: "100%", overflowY: "scroll" }}>
          <InfiniteScroll
            dataLength={items.length}
            next={fetchMoreData}
            hasMore={items.length < 200}
            loader={<h4>Loading...</h4>}
            scrollableTarget="scrollableDiv"
          >
            {items.map((i, index) => (
              <div style={style} key={index}>
                div - #{index}
              </div>
            ))}
          </InfiniteScroll>
        </div>
      );
    };
    
    

    注意添加了 'id="scrollableDiv"' 和 'scrollableTarget="scrollableDiv"'。

    【讨论】:

    • 我有类似的设置。它适用于铬。不适用于ie。有什么建议吗?
    • Ankur Marwaha - 我刚刚在 Edge 浏览器中尝试过,它是 Internet Explorer 的替代品,它运行良好,不确定 IE。
    • 适用于边缘,但不适用于 ie11。并且反向滚动也不适用于 ie11
    • 啊,我不太确定,对不起。不幸的是,我们不再在我们的项目中使用 InfiniteScroll(不是因为任何限制,但不是我们目前想要的)。我希望我能提供更多帮助。
    • 完美运行!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多