当父级重新渲染时,已连接的React组件不必要地重新渲染

时间:2019-11-19 21:53:25

标签: javascript reactjs redux react-redux

我有一个React组件,该组件通过mapStateToProps的实现连接到redux存储。 React-redux connect实现了一个浅等式的shouldComponentUpdate,如果prop不变(基于引用),它应该防止重新渲染。但是,除非我使组件显式纯净(使用PureComponent),否则当组件的父项重新呈现时,组件将重新呈现。这不应该是必需的,因为connect应该像PureComponent一样实现了shouldComponentUpdate。为什么会这样呢?具体来说,connect的实现与PureComponent有何不同?不会使包裹的组件变得纯净吗?

1 个答案:

答案 0 :(得分:0)

  

连接应该像PureComponent一样实现了shouldComponentUpdate

connect函数不会执行此操作。
PureComponent为您提供shouldComponentUpdate函数,该函数是一个占位符,您可以在其中放置性能调整代码。

coonect采用了不同的方法,并提供了4个功能/占位符,而不是 。您可以使用全部四个或部分或全部不使用来微调性能。这些函数称为:
areStatesEqual
areStatePropsEqual
areOwnPropsEqual
areMergedPropsEqual

  

不是通过连接使其包装的组件纯净吗?

通过为您提供使用上述功能的选项,一切由您自己决定。您可以模仿shouldComponentUpdate进行的浅层相等检查。

相关问题