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.
发布评论
评论(1)
我认为将所有内容都置于一种效果时没有错。使用具有不同依赖关系数组的多个效果的唯一好处是,如果只有一个参数更改,则可以跳过某些操作。
但是我认为对于您的用例并非如此,因为如果一个过滤器参数更改,则必须重新计算所有过滤器并再次排序。您唯一的好处是避免在仅排序参数更改时重新过滤。
即使那样,使用多种效果的性能提高也可能是可以忽略的。产品列表来自后端,因此我认为我们可以安全地认为它不是很大的 - 也许是数百个要素,但不是数百万或数十亿美元。过滤器操作很便宜,我们可以最后执行排序步骤。
因此,只需使用一个
使用
与组合依赖关系的挂钩。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.