当前位置:网站首页>【话题终结者】
【话题终结者】
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
边栏推荐
- XV Function definition and call
- 最短Hamilton路径 (状压DP)
- 2022 National Games RE1 baby_ tree
- Employment of cashier [differential constraint]
- All in one 1405: sum and product of prime numbers
- RTKLIB: demo5 b34f. 1 vs b33
- Fairygui bar subfamily (scroll bar, slider, progress bar)
- MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
- [algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
- Rt-ppp test using rtknavi
猜你喜欢
![[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

【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
![[untitled]](/img/b1/9a2bebebb24132a405fc4e7d854e51.png)
[untitled]

MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
![[算法] 剑指offer2 golang 面试题10:和为k的子数组](/img/63/7422489d09a64ec9f0e79378761bf1.png)
[算法] 剑指offer2 golang 面试题10:和为k的子数组

阿里云微服务(四) Service Mesh综述以及实例Istio
![[algorithm] sword finger offer2 golang interview question 10: subarray with sum K](/img/63/7422489d09a64ec9f0e79378761bf1.png)
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K

10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache

Music playback (toggle & playerprefs)

C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
随机推荐
堆排序【手写小根堆】
Exception: ioexception:stream closed
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
All in one 1405: sum and product of prime numbers
Answer to "software testing" exercise: Chapter 1
Realization of the code for calculating the mean square error of GPS Height Fitting
《软件测试》习题答案:第一章
How do architects draw system architecture blueprints?
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
记录:动态Web项目servlet访问数据库404错误之解决
Implementation of Excel import and export functions
记录:newInstance()过时的代替方法
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
Problems and solutions of robust estimation in rtklib single point location spp
面试必备:聊聊分布式锁的多种实现!
TYUT太原理工大学2022软工导论简答题
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Excel导入,导出功能实现