简体   繁体   English

带有类别过滤器和无限滚动的 React 图库

[英]React gallery with category filter and infinite scroll

I use react-photoswipe-gallery to render some images for a portfolio page.我使用 react-photoswipe-gallery 为投资组合页面渲染一些图像。 Also I made a infinite scroll functionality so it only loads 10 items initially.我还做了一个无限滚动功能,所以它最初只加载 10 个项目。 Now it works based on button and adds 10 more items onClick, but I plan to change it to scroll.现在它基于按钮工作并增加了 10 个项目 onClick,但我计划将其更改为滚动。 Everything works as intended until this point, this is how the gallery and inifinite scroll works:到目前为止,一切都按预期工作,这就是画廊和无限滚动的工作方式:

    const loadLimit = 10;
    const [galleryItems, setGalleryItems] = useState(
        galleryData.slice(0, loadLimit)
    );
    const [visibleItems, setVisibleItems] = useState(loadLimit);
    const [selectedOption, setSelectedOption] = useState([]);

    const loadMore = () => {
        const newVisible = visibleItems + loadLimit;
        const newItems = galleryItems.concat(
            galleryData.slice(visibleItems, newVisible)
        );

        setVisibleItems(newVisible);
        setGalleryItems(newItems);
    };

Since there will be a lot of images, I'd like to also add a category filter using react-select with isMulti enabled.由于会有很多图像,我还想使用启用isMulti的 react-select 添加一个类别过滤器。 From what I've checked in console I target the right things, the category is set correctly.从我在控制台中检查的内容来看,我的目标是正确的,类别设置正确。 It only works when nothing is selected - by showing every image from any type of category.它仅在未选择任何内容时才有效 - 通过显示来自任何类型类别的每个图像。 Once I select a category I need to click the Load more button and it shows every image available again, not by category.一旦我 select 一个类别,我需要单击加载更多按钮,它会再次显示每个可用的图像,而不是按类别。

    useEffect(() => {
        if (selectedOption.length === 0)
            return setGalleryItems(galleryData.slice(0, loadLimit));

        const filteredData = galleryData
            .filter((item) => item.category === selectedOption.value)
            .slice(0, loadLimit);

        setGalleryItems(filteredData);
    }, [galleryItems, selectedOption]);

    <Select
        components={makeAnimated()}
        options={filterOptions}
        value={selectedOption}
        onChange={setSelectedOption}
        placeholder="Filter by category"
        isMulti
    />

selectedOptions is a simple array with the category names. selectedOptions是一个带有类别名称的简单数组。

galleryItems is an array of objects structured like this: galleryItems是一个结构如下的对象数组:

    {
        source: imageSource,
        category: 'category-name'
    }

If that 1st if statement returns true, you'll receive the unfiltered images, and in that statement you're comparing against selectedOption.length but later using selectedOption.value .如果第一个 if 语句返回 true,您将收到未过滤的图像,并且在该语句中,您将与selectedOption.length进行比较,但稍后使用selectedOption.value I suspect that's a bug, except (undefined === 0) equates to false, so there may be something else going on as well.我怀疑这是一个错误,除了(undefined === 0)等于 false,所以可能还有其他事情发生。

Also, it load more code is setting galleryItems without taking the filter into account.此外,它加载更多代码是在不考虑过滤器的情况下设置 galleryItems。 And when load more calls "setGalleryItems()" it may be triggering your useEffect() hook to run again which will then clobber the gallery.当加载更多调用“setGalleryItems()”时,它可能会触发你的 useEffect() 钩子再次运行,这将破坏画廊。

Typically, the 'load next' code will just bump the page # and nothing else.通常,“加载下一个”代码只会撞到页面 # 而不是别的。 The page number will be a dependency on the useState() hook (not the page data), and inside useState() hook populate the gallery with the right page data.页码将依赖于 useState() 挂钩(而不是页面数据),并且在 useState() 挂钩内部使用正确的页面数据填充图库。

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

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