将庞大的数据对象保存在 Redux 的单个存储中是个好主意吗? [英] Is it a good idea to keep huge data object in a single store of Redux?

查看:51
本文介绍了将庞大的数据对象保存在 Redux 的单个存储中是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Redux 提供了 store 用于将所有应用程序数据保存在一个地方,将数据保存在单个大型 object 中是否是一种好习惯?如果我们有数千条记录,而且它们的数据很大,会不会影响应用程序的性能?

As Redux provides store for keeping all application data in one place, Is it good practice to keep data in single large object? What if we have thousands of records and their data which is large in size, will it affect on application performance?

推荐答案

这是一个很好的起点Redux 性能

为了在 React 应用程序中获得最大的渲染性能,状态应该以规范化的形状存储,许多单独的组件应该连接到存储而不是几个,并且连接的列表组件应该传递 item其连接的子列表项的 ID(允许列表项通过 ID 查找自己的数据).这最大限度地减少了要完成的渲染的总量.使用记忆化选择器函数也是一个重要的性能考虑因素.

For maximum rendering performance in a React application, state should be stored in a normalized shape, many individual components should be connected to the store instead of just a few, and connected list components should pass item IDs to their connected child list items (allowing the list items to look up their own data by ID). This minimizes the overall amount of rendering to be done. Use of memoized selector functions is also an important performance consideration.

关于Redux

不可变地更新状态通常意味着制作浅拷贝,而不是深拷贝.浅拷贝比深拷贝快得多,因为需要复制的对象和字段更少,而且它实际上归结为移动一些指针.

Immutably updating state generally means making shallow copies, not deep copies. Shallow copies are much faster than deep copies, because fewer objects and fields have to be copied, and it effectively comes down to moving some pointers around.

但是,您确实需要为受影响的每个嵌套级别创建一个复制和更新的对象.尽管这不应该特别昂贵,但这是您应该尽可能保持状态规范化和浅层的另一个很好的理由.

However, you do need to create a copied and updated object for each level of nesting that is affected. Although that shouldn't be particularly expensive, it's another good reason why you should keep your state normalized and shallow if possible.

正如@MatanHafuta 所提到的,状态对象的外观非常重要.

As mentioned by @MatanHafuta, it is very important how your state object looks like.

您可以使用 normalizr 之类的包来规范化具有深层嵌套对象的 JSON 数据.

You can use a package like normalizr to normalize JSON data that has deeply nested objects.

这篇关于将庞大的数据对象保存在 Redux 的单个存储中是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆