简体   繁体   English

React Native 如何在集合中的每个组件上使用 React.memo 以仅渲染更改的组件。 React.memo 的奇怪行为

[英]React Native how to use React.memo on every component from collection to render only changed ones. Strange behavior of React.memo

I have been trying to improve rendering performance by using React.memo on components.我一直在尝试通过在组件上使用 React.memo 来提高渲染性能。 It works properly until I use it on every single component from the collection being inserted as children.它可以正常工作,直到我在作为子项插入的集合中的每个组件上使用它为止。

I prepared snack HERE我在这里准备了小吃

To understand what is going on you need to open the console because there are some logs presented.要了解发生了什么,您需要打开控制台,因为显示了一些日志。 You will see WrapperMemo which generally renders Child components (they are also wrapped in some auxiliary components to check something I will explain later).您将看到WrapperMemo通常呈现组件(它们也被包裹在一些辅助组件中以检查我稍后会解释的内容)。

When you press the first button named SLICE MYNUMBERS you will see that React.memo's isEqual function called isWrapperEqual returns true, but it still does not stop from rerendering Child components.当你按下第一个名为SLICE MYNUMBERS的按钮时,你会看到 React.memo 的 isEqual function 调用isWrapperEqual返回 true,但它仍然不会停止重新渲染组件。 However neither Wrapper nor all other auxiliary wrapper components ( ChildOk ) are rendered!然而,无论是Wrapper还是所有其他辅助包装组件( ChildOk )都不会被渲染!

When you click any PRESS ME:[number] button it forces you to re-render other Child components when only one is being modified.当您单击任何PRESS ME:[number]按钮时,它会强制您在仅修改一个子组件时重新渲染其他组件。

It is worth mentioning that TestCompMemo is never re-rendered when any of the buttons are pressed - which is correct behavior.值得一提的是,当任何按钮被按下时, TestCompMemo永远不会重新呈现——这是正确的行为。

1) Is there any explanation for that? 1)对此有什么解释吗?

2) Is there a way to use React.memo on every component from collection to render only changed ones (as it is presented in Wrapper component)? 2) 有没有办法在集合中的每个组件上使用 React.memo 以仅呈现更改的组件(因为它在Wrapper组件中呈现)?

  1. Is there any explanation for that?对此有什么解释吗?

In your snack example, you have a Context.Provider wrapper at the top level of the application.在您的零食示例中,您在应用程序的顶层有一个Context.Provider包装器。

This is what causes the re-render.这就是导致重新渲染的原因。 This is expected behavior and you can read more about it here: https://github.com/facebook/react/issues/15156#issuecomment-474590693这是预期的行为,您可以在此处阅读更多信息: https://github.com/facebook/react/issues/15156#issuecomment-474590693

  1. Is there a way to use React.memo on every component from collection to render only changed ones (as it is presented in Wrapper component)?有没有办法在集合中的每个组件上使用 React.memo 以仅呈现更改的组件(因为它在 Wrapper 组件中呈现)?

Using React.memo on every component is not recommended.不建议在每个组件上都使用 React.memo。 You can run into bugs where components do not re-render and you are adding overhead with all the comparisons.您可能会遇到组件不重新渲染的错误,并且您会增加所有比较的开销。

If you want to optimize the application, you might want to run the profiler and figure out which parts of the app is slowing things down and work from there.如果您想优化应用程序,您可能需要运行分析器并找出应用程序的哪些部分正在减慢速度并从那里开始工作。

But if you want to prevent unnecessary re-renders, you could move context usage up a few levels and pass the onPress to the child components.但是,如果您想防止不必要的重新渲染,您可以将上下文使用提高几个级别并将onPress传递给子组件。

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

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