【发布时间】:2019-11-26 10:01:54
【问题描述】:
我有一个基于 React 和 Redux 的聊天。
聊天容器接收 messageIds 并使用 messageIds.map() 为每条消息呈现组件。该组件连接到 Redux,因此它接收 messageId 并仅在 messageId 更改时更新。
但在分析器中,我在收到新消息时看到了很多(React Tree Reconciliation: Completed Root)事件,大约需要 6 毫秒。如何防止这种和解或如何优化它?
例如,我在聊天中有 30 条消息,并且每条新消息的协调大约需要 6 毫秒 * 30 条消息 = 180 毫秒,即使消息组件没有重新呈现也是如此。下面我提供了 profiler 的截图。
【问题讨论】:
-
将消息组件包装在 React.memo 中,或者如果消息组件是一个类,则从 React.PureComponent 继承。我需要查看消息组件及其容器以提供更多有用的建议。
-
React 端组件的@HMR 已优化,我也在使用 shouldComponentUpdate 和 PureComponent,但是通过分析器,看起来 Redux 试图更新消息组件。在探查器中,我看到: 1. Connect(MessageItemContainer) [更新] 1.2。 Connect(MessageItemContainer).shouldComponentUpdate 1.3。 Connect(MessageItemContainer).componentWillUpdate 1.4。连接(消息项容器).getChildContext 1.5。 MessageItemContainer.shouldComponentUpdate
-
如果你不是从 React.PureComponent 扩展而不是实现 shouldComponentUpdate 比我想看到 shouldComponentUpdate 和道具来自哪里以及你从父级传递的道具消息项。