React 组件之 Component PureComponent Function Component

Virtual DOM , 通过高效的Diff算法对变化的部分首尾两端做批量更新,所有的比较都是浅比较shallowEqual。谁都玩不起深比较,facebook自己都做不到~

Component :一定要配套使用shouldComponentUpdate , 否则不管props or state是否更新组件一定更新

pureComponent :当组件的props和state不变时,组件是不更新的。仅仅只需要替换component => pureComponent,零投入超高回报

function Component:写纯函数组件非常简洁优雅,官方也推荐这种写法。但是,这并不代表纯函数组件是性能最好的组件写法。
在内部被包装成了一个只有render方法的StatelessComponent组件,在所有情况下都会更新。
(facebook说过要优化StatelessComponent,要优化它的性能也很简单,但就是一直没优化,也不知道为什么。后来我才知道有了终极解决方案,React Hook)
因此,并不建议使用它写比较复杂的组件

总结:

PureComponent > StatelessComponent > Component

function Component vs PureCompoent ,不建议比较复杂的页面,使用function Component.

参考知乎链接:React组件性能优化实例解析 https://zhuanlan.zhihu.com/p/34632531

原文地址:https://www.cnblogs.com/ajaxkong/p/11419061.html

时间: 2024-08-01 15:31:31

React 组件之 Component PureComponent Function Component的相关文章

[React] Preventing extra re-rendering with function component by using React.memo and useCallback

Got the idea form this lesson. Not sure whether it is the ncessary, no othere better way to handle it. Have a TodoList component, every time types in NewTodo input fields, will trigger the re-rendering for all components. import React, { useState, us

精读《Function Component 入门》

1. 引言 如果你在使用 React 16,可以尝试 Function Component 风格,享受更大的灵活性.但在尝试之前,最好先阅读本文,对 Function Component 的思维模式有一个初步认识,防止因思维模式不同步造成的困扰. 2. 精读 什么是 Function Component? Function Component 就是以 Function 的形式创建的 React 组件: function App() { return ( <div> <p>App&l

React 的 PureComponent Vs Component

一.它们几乎完全相同,但是PureComponent通过prop和state的浅比较来实现shouldComponentUpdate,某些情况下可以用PureComponent提升性能 1.所谓浅比较(shallowEqual),即react源码中的一个函数,然后根据下面的方法进行是不是PureComponent的判断,帮我们做了本来应该我们在shouldComponentUpdate中做的事情 if (this._compositeType === CompositeTypes.PureCla

react常见组件问题Can&#39;t perform a React state update on an unmounted component

在些react组件的时候,会有这个警告 Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method” 这是因为在写一

react项目Bug:组件销毁清除监听(Can&#39;t perform a React state update on an unmounted component.)

最近一直在做react项目,发现一个bug,困扰了我两天. Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount metho

[Vue warn]: You may have an infinite update loop in a component render function

[Vue warn]: You may have an infinite update loop in a component render function 这个问题很奇怪,之前从来没有遇到过.如果是我自己主导的项目,倒也好办,慢慢 debug 就是:偏偏在公司的项目里遇到这个问题,而公司项目的体系结构很复杂,我还没完全掌握.更恼火的是,因为体系复杂,debug 也非常困难,再加上尚无测试框架,这个难搞啊…… 好死不死的,当时是下午3.4点钟,正好到了肚饿的时刻,结果又落入低血糖状态,真是屋漏

谈谈Ext JS的组件——组件基类:Ext.Component

概述 Ext.Component是所有Ext组件的基类,这在Ext.Component的API中第一句话就提到了.然后第二段说明了它包含的基本功能:隐藏/显示.启用/禁用以及尺寸控制等.除了以上这些基本功能,其实还包含了很多东西.当然,在API中不可能面面俱到,那么,我们应当如何去理解这个组件基类呢? Ext.Component===DIV 如果将一个Ext.Component渲染到页面,会看到该组件会简单的在页面中添加一个DIV标记,就是这么简单.也就是说,Ext.Component就相当于一

IBM特别开发出一套全新的企业模型-- 组件化商业模式(Component Business Model,CBM)

我有一个想法:EOM 我今天在对解决方案作领域分解(domain decomposition)的时候,想到从企业运营的角度出发,能不能提出一个企业运营图EOM(Enterprise Operation Map)的这么一个概念.EOM是一种业务过程模型或者框架,它为企业提供所要求的企业过程,它从企业的业务视图出发来描述需求,对业务过程进行分析和设计:再经过系统分析和设计,形成解决方案的分析和设计,最终建立系统并投入使用.EOM的重点在于对企业开展业务所涉及的过程单元和业务活动进行分类,并通过多种方

React组件开发注意事项

0.state的设定原则,如果render里用不到,则就不应该是一个state. 1.数组遍历时,用每一条数据的唯一标识作为key,尽量不要使用遍历的索引值作为key,如果它们从不重新排序,它们工作也很好,但是如果存在重新排序,性能将会很差. 2.处理事件,推荐使用属性初始化语法,如下: class LoggingButton extends React.Component { // 这个语法确保 `this` 绑定在 handleClick 中. // 警告:这是 *实验性的* 语法. han