简体   繁体   English

React / Redux获取和过滤数据

[英]React / Redux fetching and filtering data

Lets say for example I have a giant list of items lets call it contacts this is 1000+ items in a list we have bunch of filters such as contact type , contact location , assigned to , filter ASC, filter DESC`. 可以说,例如,我有一个庞大的项目列表,可以称之为“ contacts这是列表中的1000多个项目,我们有很多过滤器,例如contact typecontact locationassigned tofilter ASC,过滤器DESC`。 That a user can input whatever they want. 用户可以输入他们想要的任何内容。 The redux store consists of contacts in a normalized object Redux存储由标准化对象中的联系人组成

{
  "1": {
    "name": "Home Simpson",
    "type": "Lead",
    "location": "California",
    "created_at": "01/01/16"
  },
  "2": {
    "name": "Ned Flanders",
    "type": "Client",
    "location": "SpringField",
    "created_at": "05/01/16"
  },
  [...1000+]
}

After fetching all of the contacts is it better to map and filter over all of the contacts on the client side based off of the user input? 提取所有联系人之后,是否更好地根据用户输入来映射和筛选客户端上的所有联系人?

Or should we be making another request to the server to get all of the contacts related to the specific filters? 还是我们应该向服务器发出另一个请求,以获取与特定过滤器相关的所有联系人?

Note that it is not just one param that can be queried for it is multiple params. 请注意,不仅可以查询一个参数,还可以查询多个参数。 Hence contact.type === : "Lead" || 因此contact.type ===:“ Lead” || "Client", and contact.location === "Spring Field" “客户端”,和contact.location ===“弹簧场”

What are best practice for a query of this size and is making trips to the server for all the matching contacts worth the extra request or is it better to filter our redux store client side and not put the load on the server? 进行这种规模查询的最佳实践是什么,是否值得所有额外匹配的联系人前往服务器,还是最好过滤掉我们的redux store客户端而不将负载加到服务器上?

In terms of Redux: feel free to do this client side. 在Redux方面:可以随意在客户端进行操作。 This kind of filtering will be very fast, and Redux isn't going to slow it down. 这种过滤非常快,Redux不会减慢它的速度。

In general, this is the type of thing that you don't want to optimize unless you actually know its going to be a problem. 通常,除非您真正知道它会成为问题,否则这是您不希望优化的事情。 When you're talking about maybe there being an off-chance of someone having a data set too large many years down the road, this is a grossly premature optimization--there are probably dozens of things you're better served working on instead of this. 当您谈论的是也许某个人在未来许多年内可能拥有过大数据集的机会时,这是一个过早的优化-可能有很多事情最好为您服务而不是这个。 For all you know, payload size might be a much bigger problem than this (how big are the contact objects?) 众所周知,有效载荷的大小可能比这大得多(接触对象有多大?)

But no need to take anyone's word for it. 但是,无需任何人相信。 Generate a data set on a target device (what would your typical user be using? What about worst case?), and do performance benchmarks filtering the sample data. 在目标设备上生成数据集(您的典型用户将使用什么?最坏情况如何?),性能基准是否会过滤示例数据。 I have a feeling you're going to discover that the kind of filtering you're doing is not going to be a bottleneck, considering it is just O(n* 1). 我有一种感觉,您会发现正在执行的过滤类型不会成为瓶颈,因为它只是O(n * 1)。 You're filtering through n elements O(n), checking a single value on each O(1). 您正在过滤n个元素O(n),检查每个O(1)上的单个值。

On the other hand, if you have a list of very complicated objects with a very complicated filter, your result might change. 另一方面,如果您具有包含非常复杂的过滤器的非常复杂的对象的列表,则结果可能会更改。 For example if you're looking for all contacts that know all other contacts who know the same three specific people, the complexity will go up and you're more likely to run into a bottleneck. 例如,如果您要查找认识其他所有认识同一三个特定联系人的所有其他联系人,则复杂性将会增加,并且您更有可能遇到瓶颈。

In any case, I really recommend trying to benchmark yourself before spending a ton of time prematurely optimizing your application. 无论如何,我真的建议您在花费大量时间过早地优化应用程序之前尝试对自己进行基准测试。

IMO, the right place to filter data is not directly in the reducers but in the selectors . IMO, 过滤数据的正确位置不是直接在化简中,而是在选择器中

From redux docs: 来自redux文档:

Computing Derived Data 计算衍生数据

Reselect is a simple library for creating memoized, composable selector functions. 重新选择是一个简单的库,用于创建可记忆的可组合选择器函数。 Reselect selectors can be used to efficiently compute derived data from the Redux store. 重新选择选择器可用于有效地计算来自Redux存储的派生数据。

I'm currently using selectors to filter and sort data. 我目前正在使用选择器来过滤和排序数据。

  1. No data repetition in the state. 该状态下无数据重复。 You don't have to store a copy of the items filtered by one specific way. 您不必存储通过一种特定方式过滤的项目的副本。
  2. The same data can be used in different components, each one using a different selector function to filter. 相同的数据可用于不同的组件,每个组件使用不同的选择器功能进行过滤。
  3. You can combine selector applying many data computations using selector that you already have in the application. 您可以使用应用程序中已有的选择器来组合应用许多数据计算的选择器。
  4. If you do right, your selectors will be pure functions, then you can easily test them. 如果操作正确,选择器将是纯函数,则可以轻松对其进行测试。
  5. Use the same selector in many components. 在许多组件中使用相同的选择器。

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

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