状态是PureComponent和无状态功能组件之间的唯一区别吗?

时间:2018-02-14 18:08:30

标签: reactjs

如果我定义PureComponent

  • 只有render()方法和
  • 不使用this.state

...它是否与无状态功能组件完全相同?或者行为或表现有什么不同吗?

这不是React functional stateless component, PureComponent, Component; what are the differences and when should we use what?的副本,因为我的问题的答案并未包含在内,至少不是一种易于查明的方式。这是一个很大的,广泛的问题,我的非常具体。

1 个答案:

答案 0 :(得分:1)

无状态功能组件实际上与没有生命周期方法和状态的React.Component相同,而不是React.PureComponent

React.PureComponent的全部要点是使用生命周期方法之一(shouldComponentUpdate),并使用浅层比较,使其仅在属性和状态已更改时才返回true

在无状态功能组件中,这是无法实现的,因为它们总是被呈现并且无法定义shouldComponentUpdate

此行为在Optimizing Performance中有详细描述。