Redux 存储会导致内存泄漏吗? [英] Can a Redux store lead to a memory leak?

查看:30
本文介绍了Redux 存储会导致内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仪表板应用程序,其中有几个图表会按设定的时间间隔更新.我的第一个想法是更新商店中的数据,然后让所有图表从那里输入.

I have a dashboard application with several charts getting updated on a set interval. My first thought was to update the data in the store and then let all charts feed from there.

但这会导致内存泄漏吗?由于 Redux 每次数据更改时都会创建一个新存储并保留旧存储.每秒大约 2mb 的数据会堆积起来并导致应用程序崩溃吗?

But could that lead to a memory leak? Since Redux creates a new store every time the data changes and keeps the old ones. Would a ~2mb data every second pile up and crash the application?

我看到的替代方法是将数据保持在本地状态(使用 setState).我希望一些更有经验的 React/Redux 开发人员可以在这方面给我建议.谢谢!

The alternative I see is to keep the data in the local state (with setState). I hope some more experienced React/Redux devs can advice me on this. Thanks!

推荐答案

Dan Abramov,Redux 的创造者解决了这个问题 这里 像这样:

Dan Abramov, the creator of Redux addresses this concern here like so:

请注意,有时人们会对 Redux 感到困惑,并认为在每个操作中,都必须深入克隆状态树.事实并非如此.只有改变的部分需要改变它们的参考.例如,如果某个操作导致数组中的一项发生更改,则确实需要复制该项目和该数组,但是,该数组中的所有其他元素将保持其身份.因为大多数时候操作非常有针对性并且会影响一些状态键,并且因为 Redux 鼓励规范化数据以便数据结构不会深度嵌套,所以对于典型的 web 应用程序来说,这比人们想象的要少得多.

Note that sometimes people get confused about Redux and assume that on every action, the state tree has to be cloned deeply. This is absolutely not the case. Only the parts that changed need to change their references. For example, if an action causes a change to one item in an array, indeed, that item and the array will need to be copied, however, all other elements in the array will keep their identities. Because most of the times actions are very targeted and affect a few state keys, and because Redux encourages normalizing data so that the data structures are not deeply nested, this is much less of a problem for typical webapps than one might imagine.

我认为这就是答案.

这篇关于Redux 存储会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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