如果我定义PureComponent
render()
方法和this.state
,...它是否与无状态功能组件完全相同?或者行为或表现有什么不同吗?
这不是React functional stateless component, PureComponent, Component; what are the differences and when should we use what?的副本,因为我的问题的答案并未包含在内,至少不是一种易于查明的方式。这是一个很大的,广泛的问题,我的非常具体。
答案 0 :(得分:1)
无状态功能组件实际上与没有生命周期方法和状态的React.Component
相同,而不是React.PureComponent
。
React.PureComponent
的全部要点是使用生命周期方法之一(shouldComponentUpdate
),并使用浅层比较,使其仅在属性和状态已更改时才返回true
。
在无状态功能组件中,这是无法实现的,因为它们总是被呈现并且无法定义shouldComponentUpdate
。
此行为在Optimizing Performance中有详细描述。