【问题标题】:React native PureComponent and shouldComponentUpdate反应原生 PureComponent 和 shouldComponentUpdate
【发布时间】:2020-11-02 17:29:13
【问题描述】:

在使用 PureComponent 时,我需要知道shallow comparison 的含义。其实我看了一些文章,但我无法理解其中的含义,所以请简化它。

另一个问题是,什么时候可以使用PureComponent,什么时候可以使用shouldComponentUpdate?

【问题讨论】:

标签: reactjs react-native


【解决方案1】:

关于Shallow Comparison你可以查看这个答案

https://stackoverflow.com/questions/36084515/how-does-shallow-compare-work-in-react#:~:text=Shallow%20compare%20is%20efficient%20way,you%20don't%20mutate%20data.&text=shallow%20comparison%20is%20when%20the,comparisons%20deeper%20into%20the%20properties.

关于shouldComponentUpdate [https://en.reactjs.org/docs/react-component.html#shouldcomponentupdate]

使用 shouldComponentUpdate() 让 React 知道组件的输出是否不受当前状态或道具变化的影响。默认行为是在每次状态更改时重新渲染,在绝大多数情况下,您应该依赖默认行为。

基本上,组件每次状态变化都会重新渲染,如果你想阻止它,你可以使用shouldComponentUpdate()

关于PureComponent[https://blog.logrocket.com/pure-functional-components-in-react-16-6/]

如果 React 组件为相同的 state 和 props 呈现相同的输出,则可以认为它是纯组件。对于这样的类组件,React 提供了 PureComponent 基类。扩展 React.PureComponent 类的类组件被视为纯组件。

我强烈建议您阅读有关 React Hooks[https://en.reactjs.org/docs/hooks-intro.html] 的内容渲染。

问候。

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 2018-02-18
    • 2019-08-05
    • 2018-03-27
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多