简体   繁体   English

如何使用带有钩子的 React Infinite Scroll

[英]How to use React Infinite Scroll with hooks

First time using react-infinite-scroll-component and am struggling to get it work as intended.第一次使用react-infinite-scroll-component并且正在努力使其按预期工作。 At the moment, it calls a loadMore method after fetching the first 2 items (which is what I want), but then it loads the next 3 items (which it should only load 2) and doesn't call the loadMore method again.目前,它在获取前 2 个项目(这是我想要的)之后调用 loadMore 方法,但随后它会加载接下来的 3 个项目(它应该只加载 2 个)并且不会再次调用 loadMore 方法。 Can anyone see where i'm going wrong?谁能看到我哪里出错了?

I am passing itemsPerPage as props, eg 10, so I can set numberOfItemsToDisplay as 10, and then when loadMore gets called, I can increment numberOfItemsToDisplay with the itemsPerPage value.我将 itemsPerPage 作为道具传递,例如 10,因此我可以将 numberOfItemsToDisplay 设置为 10,然后当调用 loadMore 时,我可以使用 itemsPerPage 值增加 numberOfItemsToDisplay。

const dataList = [
    { src: 'http://ww3.sinaimg.cn/mw690/62aad664jw1f2nxvya0u2j20u01hc16p.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxvyo52qj20u01hcqeq.jpg' },
    { src: 'http://ww2.sinaimg.cn/mw690/62aad664jw1f2nxvz2cj6j20u01hck1o.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxvzfjv6j20u01hc496.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxw0e1mlj20u01hcgvs.jpg' },
    { src: 'http://ww4.sinaimg.cn/mw690/62aad664jw1f2nxw0p95dj20u01hc7d8.jpg' },
    { src: 'http://ww3.sinaimg.cn/mw690/62aad664jw1f2nxvya0u2j20u01hc16p.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxvyo52qj20u01hcqeq.jpg' },
    { src: 'http://ww2.sinaimg.cn/mw690/62aad664jw1f2nxvz2cj6j20u01hck1o.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxvzfjv6j20u01hc496.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxvyo52qj20u01hcqeq.jpg' },
    { src: 'http://ww2.sinaimg.cn/mw690/62aad664jw1f2nxvz2cj6j20u01hck1o.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxvzfjv6j20u01hc496.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxw0e1mlj20u01hcgvs.jpg' },
    { src: 'http://ww4.sinaimg.cn/mw690/62aad664jw1f2nxw0p95dj20u01hc7d8.jpg' },
    { src: 'http://ww1.sinaimg.cn/mw690/62aad664jw1f2nxvzfjv6j20u01hc496.jpg' },
];

const LazyList = ({ itemsPerPage }) => {

const [numberOfItemsToDisplay, setNumberOFItemsToDisplay] = useState(itemsPerPage);
const [hasMore, setHasMore] = useState(true);

const loadItems = () => {
    let items = numberOfItemsToDisplay;
    if (items >= dataList.length) {
        setHasMore(false);
        return;
    }
    setNumberOFItemsToDisplay((items += numberOfItemsToDisplay));
};

return (
    <div data-testid="LazyList" className={styles['pt-lazyList']}>
        <h1>test</h1>
        <InfiniteScroll
            dataLength={numberOfItemsToDisplay}
            next={loadItems}
            hasMore={hasMore}
            loader={<h4>Loading...</h4>}
        >
            {dataList.map((item, index) => {
                const id = index;
                return <img key={id} src={item.src} alt="placeholder" className={styles['pt-lazyList--image']} />;
            })}
        </InfiniteScroll>
    </div>
);
};

Any help would be really appreciated!任何帮助将非常感激! Thanks谢谢

Might not be the cleanest answer, but got it working by not mapping through the whole dataList and creating array of items based on the numberOfItemsToDisplay value:可能不是最干净的答案,但通过不映射整个 dataList 并根据 numberOfItemsToDisplay 值创建项目数组来使其工作:

const [numberOfItemsToDisplay, setNumberOFItemsToDisplay] = useState(itemsPerPage);
    const [hasMore, setHasMore] = useState(true);
    const list = [];

    for (let i = 0; i < numberOfItemsToDisplay; i += 1) {
        list.push(dataList[i]);
    }

    const loadItems = () => {
        let items = list.length;
        if (list.length >= dataList.length) {
            setHasMore(false);
            return;
        }
        setNumberOFItemsToDisplay((items += itemsPerPage));
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM