当前位置:网站首页>【话题终结者】
【话题终结者】
2022-07-06 09:19:00 【安鱼哥】
写在前面
CSDN话题挑战赛第1期
活动详情地址:CSDN
参赛话题:前端面试宝典
题目一:在 React中元素( element)和组件( component)有什么区别?
答案:
简单地说,在 React中元素(虛拟DOM)描述了你在屏幕上看到的DOM元素。
换个说法就是,在 React中元素是页面中DOM元素的对象表示方式。在 React中组件是一个函数或一个类,它可以接受输入并返回一个元素。
注意:工作中,为了提高开发效率,通常使用JSX语法表示 React元素(虚拟DOM)。在编译的时候,把它转化成一个 React. createElement调用方法。
在这里我们编写针对题目一的答案。
扩展:
什么是 React的refs?为什么它们很重要?
refs允许你直接访问DOM元素或组件实例。为了使用它们,可以向组件添加个ref属性。
如果该属性的值是一个回调函数,它将接受底层的DOM元素或组件的已挂载实例作为其第一个参数。可以在组件中存储它。
export class App extends Component {showResult ( ) {console. log(this. input. value)}render ( ) {return (<div><input type="text" ref={input => this .input =input } />< button onClick={this. showResult.bind(this)}>展示结果</ button></div>);}}
如果该属性值是一个字符串, React将会在组件实例化对象的refs属性中,存储一个同名属性,该属性是对这个DOM元素的引用。可以通过原生的 DOM API操作它。
export class App extends Component {showResult( )console .log ( this.refs.username.value)rende
题目二:
当调用 setState的时候,发生了什么操作?
答案:
当调用 setState时, React做的第一件事是将传递给setState的对象合并到组件的当前状态,这将启动一个称为和解( reconciliation)的过程。
和解的最终目标是,根据这个新的状态以最有效的方式更新DOM。
为此, React将构建一个新的 React虚拟DOM树(可以将其视为页面DOM元素的对象表示方式)。
一旦有了这个DOM树,为了弄清DOM是如何响应新的状态而改变的, React会将这个新树与上一个虚拟DOM树比较。
这样做, React会知道发生的确切变化,并且通过了解发生的变化后,在绝对必要的情况下进行更新DOM,即可将因操作DOM而占用的空间最小化。
写在最后
CSDN话题挑战赛第1期
- 活动详情地址:CSDN
边栏推荐
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
- Record: newinstance() obsolete replacement method
- TYUT太原理工大学2022“mao gai”必背
- 【快趁你舍友打游戏,来看道题吧】
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- Problems and solutions of robust estimation in rtklib single point location spp
- Wechat applet development experience
- First acquaintance with C language (Part 2)
- Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
- Compile GDAL source code with nmake (win10, vs2022)
猜你喜欢

Fairygui bar subfamily (scroll bar, slider, progress bar)

平衡二叉树详解 通俗易懂
![[GNSS data processing] Helmert variance component estimation analysis and code implementation](/img/4e/ff0334cf9a2a37096778a8c5719a4e.jpg)
[GNSS data processing] Helmert variance component estimation analysis and code implementation

微信小程序开发心得

Dark chain lock (lca+ difference on tree)

TYUT太原理工大学2022数据库之关系代数小题
![[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
![[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal](/img/11/ee0628a68542236fc641966579a31a.png)
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal

Fgui project packaging and Publishing & importing unity & the way to display the UI

服务未正常关闭导致端口被占用
随机推荐
MySQL backup -- common errors in xtrabackup backup
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
如何保障 MySQL 和 Redis 的数据一致性?
[算法] 剑指offer2 golang 面试题1:整数除法
TYUT太原理工大学2022数据库大题之E-R图转关系模式
TYUT太原理工大学2022数据库题库选择题总结
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Error: symbol not found
TYUT太原理工大学2022“mao gai”必背
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
初识指针笔记
服务未正常关闭导致端口被占用
堆排序【手写小根堆】
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
4.30动态内存分配笔记
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
wsl常用命令
GNSS positioning accuracy index calculation