lodash iSqual函数正在引起应用程序冻结
我正在创建一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 ofReact.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 provideReact.memo
with a customareEquals
function anymore because the shallow comparison that React will do per default will suffice.