过滤产品取决于用户选择

发布于 2025-02-09 03:37:37 字数 1399 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2025-02-16 03:37:37

我认为将所有内容都置于一种效果时没有错。使用具有不同依赖关系数组的多个效果的唯一好处是,如果只有一个参数更改,则可以跳过某些操作。

但是我认为对于您的用例并非如此,因为如果一个过滤器参数更改,则必须重新计算所有过滤器并再次排序。您唯一的好处是避免在仅排序参数更改时重新过滤。

即使那样,使用多种效果的性能提高也可能是可以忽略的。产品列表来自后端,因此我认为我们可以安全地认为它不是很大的 - 也许是数百个要素,但不是数百万或数十亿美元。过滤器操作很便宜,我们可以最后执行排序步骤。

因此,只需使用一个使用与组合依赖关系的挂钩。

 useEffect(() => {

    const applySort = (filteredList) => (sort.length === 0)
       ? filteredList
       : sort.includes('price.highest') 
       ? filteredList.slice().sort((a, b) => b.price - a.price)
       : filteredList.slice().sort((a, b) => a.price - b.price);
       
    const sizePredicate = (product) => (size.length === 0) 
      || product.size.includes(Number(...size));
        
    const brandPredicate = (product) => (brand.length === 0)
       || product.brand.includes(Number(...brand));

    const pricePredicate = (product) => (price.length === 0)
       || product.price > price[0][0]   
       && product.price < price[0][1];
 
    setFilteredProducts(
      applySort(
        data.getProducts()
          .filter(sizePredicate)
          .filter(brandPredicate)
          .filter(pricePredicate)
        )
      );
  
  }, [data.getProducts(), sort, price, brand, size]);

I think there is nothing wrong about putting everything in one effect. The only benefit of using multiple effects with different dependency arrays is, that in case only one parameter changes, some operations can be skipped.

But I think that is not true for your use case, since if one filter parameter changes, you have to re-calculate all filters and sort again. The only benefit you have would be to avoid re-filtering if only sort parameters change.

Even then, the performance gains of using multiple effects would likely be neglectable. The product list comes from the backend, so I think we can safely assume it is not huge - maybe hundreds of elements, but not millions or billions. The filter operations are cheap and we can perform the sort step last.

So just use one useEffect hook with the combined dependencies.

 useEffect(() => {

    const applySort = (filteredList) => (sort.length === 0)
       ? filteredList
       : sort.includes('price.highest') 
       ? filteredList.slice().sort((a, b) => b.price - a.price)
       : filteredList.slice().sort((a, b) => a.price - b.price);
       
    const sizePredicate = (product) => (size.length === 0) 
      || product.size.includes(Number(...size));
        
    const brandPredicate = (product) => (brand.length === 0)
       || product.brand.includes(Number(...brand));

    const pricePredicate = (product) => (price.length === 0)
       || product.price > price[0][0]   
       && product.price < price[0][1];
 
    setFilteredProducts(
      applySort(
        data.getProducts()
          .filter(sizePredicate)
          .filter(brandPredicate)
          .filter(pricePredicate)
        )
      );
  
  }, [data.getProducts(), sort, price, brand, size]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文