lodash iSqual函数正在引起应用程序冻结

发布于 2025-02-09 05:10:47 字数 208 浏览 2 评论 0原文

我正在创建一个React组件,并且它像以下几行一样执行默认导出:

react.memo(myreareactcomponent,Isequal)

均等函数来自lodash lib。我的应用程序不断尝试以10 Hz更新组件道具。但是,我的应用程序不时地冻结了从lodash执行iSqual函数。你们有可能导致它的想法吗?

I am creating a react component, and it do the default export as in the following line:

React.memo(MyReactComponent, isEqual)

The isEqual function is from lodash lib. My application constantly tries to update the component props at 10 Hz. However, from time to time my application freezes executing the isEqual function from lodash. Do you guys have ideas for what could cause it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2025-02-16 05:10:47

JavaScript对象可以(从理论上)具有环状引用,因此这可能会崩溃lodash.isequal()

另一种情况是您比较的对象包含REACT组件,可能会崩溃 lodash.isequal

但是,无论您在特殊情况下可能是什么特定原因,通常在are equal() react.memo中使用深度对象比较通常不是一个好主意。毕竟,您正在使用回忆来防止昂贵的重新租赁。如果做代价高昂的道具比较,您将不会赢得很多。

一种更好的方法是优化代码,然后生成/修改组件的道具和使用usememo(),以防止对您的状态进行不必要的更新。如果您始终如一地执行此操作,则无需再提供react.memo使用自定义 are equals 函数,因为react react会进行的浅层比较将足够。

Javascript objects can (in theory) have cyclic references, so this might crash lodash.isEqual().

Another scenario is that the objects you compare contain React components, which might crash lodash.isEqual too.

But whatever the specific reason might be in your particular case, it is usually not a good idea to use deep object comparison in the areEqual() function of React.memo. After all, you are using memoization to prevent costly re-renders. If do costly prop comparison instead, you don't win a lot.

A better approach would be to optimize the code then generates/modifies the props of your component und use useMemo() to prevent unnecessary updates to your state. If you do this consistently, you don't need to provide React.memo with a custom areEquals function anymore because the shallow comparison that React will do per default will suffice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文