简体   繁体   English

如何在 Redux 中处理列表项

[英]How to handle list items in Redux

I have a list view, and a detail view on my application.我有一个列表视图和我的应用程序的详细视图。

/list pulls down some general data about a bunch of objects and throws it into an array in redux /list 拉取一些关于一堆对象的一般数据并将其扔到 redux 中的一个数组中

/list/5 grabs the details for one of the items on the list /list/5 获取列表中一项的详细信息

/list is always called no matter what route you hit on the site, so the list eventually populates无论您在网站上点击什么路线,总是会调用 /list,因此该列表最终会填充

I figured it would be best to store all of the details of the item when you hit the details route right in the items indexed location in the list array.我认为最好在您在列表数组中的项目索引位置点击详细信息路由时存储项目的所有详细信息。

the problem is that the list takes longer to be retrieved than the items details if you go to the item details route first.问题是,如果您首先转到项目详细信息路线,则检索列表所需的时间比项目详细信息要长。 I don't know what that items index will be in the array when I try to update it's location in redux.当我尝试更新它在 redux 中的位置时,我不知道数组中的项目索引是什么。

Am I handling this wrong?我处理这个错误吗? Should I put the details for the items in a separate place in the store?我应该把商品的详细信息放在商店的一个单独的地方吗? My thinking was that if the user looks at details for an item twice I can check to see if the additional data is already in the list array index, and so I don't need to make another api call.我的想法是,如果用户查看一个项目的详细信息两次,我可以检查附加数据是否已经在列表数组索引中,因此我不需要再次调用 api。 it's just already there, but if it isn't then make the call and pull the additional details它只是已经存在,但如果没有,则拨打电话并获取其他详细信息

Not sure if i'm explaining this right but i've been trying to find the right pattern for storing things with redux.不确定我是否正确解释了这一点,但我一直在努力寻找使用 redux 存储东西的正确模式。

Quoting from my answer at https://stackoverflow.com/a/40898120/62937 :引用我在https://stackoverflow.com/a/40898120/62937 上的回答:

Yes, a normalized Redux store is the standard recommendation.是的,标准化的 Redux 存储是标准建议。 See Redux FAQ: How do I organize nested or duplicate data in my state?请参阅Redux 常见问题解答:如何在我的状态中组织嵌套或重复的数据? , Structuring Reducers - Normalizing State Shape , and the Selectors and Normalization part of my React/Redux links list for more information. 结构化减速器 - 规范化状态形状,以及我的 React/Redux 链接列表的选择器和规范化部分以获取更多信息。

For manipulating relational/normalized data in your Redux store, I recommend a library called Redux-ORM .为了操作 Redux 存储中的关系/规范化数据,我推荐一个名为Redux-ORM的库。 You should absolutely use Reselect in general, and Normalizr is good for normalizing data you've received, but Redux-ORM provides a useful abstraction layer for querying and updating that normalized data once it's in the store.一般来说,您绝对应该使用 Reselect,Normalizr 可以很好地规范化您收到的数据,但 Redux-ORM 提供了一个有用的抽象层,用于查询和更新存储中的规范化数据。 I've written a couple blog posts describing its use: Redux-ORM Basics and Redux-ORM Concepts and Techniques .我写了几篇博客文章来描述它的用法: Redux-ORM 基础知识Redux-ORM 概念和技术

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

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