当前位置:网站首页>【话题终结者】
【话题终结者】
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
边栏推荐
- 167. Sum of two numbers II - input ordered array - Double pointers
- 初识C语言(上)
- 图书管理系统小练习
- 如何保障 MySQL 和 Redis 的数据一致性?
- 编辑距离(多源BFS)
- Realization of the code for calculating the mean square error of GPS Height Fitting
- Novatel board oem617d configuration step record
- 微信小程序开发心得
- [算法] 劍指offer2 golang 面試題2:二進制加法
- [algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
猜你喜欢

抽象类和接口
![[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity](/img/9d/7284c1399964d3fb48886f12e4941c.jpg)
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity

Edit distance (multi-source BFS)

121道分布式面试题和答案

阿里云微服务(三)Sentinel开源流控熔断降级组件

系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries

How to ensure data consistency between MySQL and redis?

十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩

MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景

国企秋招经验总结
随机推荐
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
121道分布式面试题和答案
TYUT太原理工大学2022数据库之关系代数小题
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
WSL common commands
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
How to reduce the shutdown time of InnoDB database?
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
RTKLIB: demo5 b34f. 1 vs b33
记录:Navicat Premium初次无法连接数据库MySQL之解决
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
GPS高程拟合抗差中误差的求取代码实现
[algorithm] sword finger offer2 golang interview question 1: integer division
最短Hamilton路径 (状压DP)
Record: solution of 404 error of servlet accessing database in dynamic web project
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix