当前位置:网站首页>15_ Key function and principle
15_ Key function and principle
2022-07-26 23:52:00 【ID does not exist.】
key Function and principle
Interview questions :react、vue Medium key What's the role ?(key Internal principle )
1. fictitious DOM in key The role of :
key Is a virtual DOM Identification of the object , When the data in the state changes ,vue Will be based on 【 The new data 】 Generate 【 New virtual DOM】
And then vue Conduct 【 New virtual DOM】 And 【 Old virtual DOM】 Comparison of differences , The comparison rules are as follows :
2. Comparison rules :
(1) Old virtual DOM Found with the new virtual DOM same key:
①. If virtual DOM The content in the has not changed , Directly use the previous reality DOM!
②. If virtual DOM The content has changed , Then a new reality is generated DOM, Then replace the previous real... In the page DOM.
(2) Old virtual DOM The new virtual machine was not found in DOM same key: Create a new reality DOM, Then render to the page .
3. use index As key Problems that may arise :
(1) If the data is : Add in reverse order 、 Reverse order deletion and other destructive operations : It creates unnecessary reality DOM to update ==> The interface effect is OK , But low efficiency .
(2) If the structure also contains... Of the input class DOM: There will be mistakes DOM to update ==> There is a problem with the interface .
4. How to choose key?
(1) It is best to use the unique identification of each data as key, such as id、 cell-phone number 、 ID number 、 Unique value such as student number .
(2) If there is no reverse order addition to the data 、 Reverse order deletion and other destructive operations , For rendering only, the list is used to show , Use index As key There is no problem .
<body>
<div id="root">
<!-- Traversal array -->
<h2> Personnel list ( Traversal array )</h2>
<button @click.once="add"> Add an old Liu </button>
<ul>
<li v-for="(p,index) of persons" :key="p.id">
{
{p.name}}-{
{p.age}}
<input type="text">
</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el: '#root',
data: {
persons: [{
id: '001',
name: ' Zhang San ',
age: 18
},
{
id: '002',
name: ' Li Si ',
age: 19
},
{
id: '003',
name: ' Wang Wu ',
age: 20
}
]
},
methods: {
add() {
const p = {
id: '004',
name: ' Lao Liu ',
age: 40
}
this.persons.unshift(p)
}
},
})
</script>
边栏推荐
- Question 152: product maximum subarray
- Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management
- 2022.7.18-----leetcode.749
- ES6新特性
- 力扣155题,最小栈
- Number that cannot be bought
- 会议OA之我的会议
- Typescript notes
- Custom type
- [interview: concurrent Article 27: multithreading: hesitation mode]
猜你喜欢

会议OA之我的会议

1. Configuration environment and project creation

Hcip day 2_ HCIA review comprehensive experiment

Part II - C language improvement_ 13. Recursive function

文件上传到OSS文件服务器

C语言数组

NFT展示指南:如何展示你的NFT藏品

How to transfer the GPX data collected by CTI RTK out of KML and SHP with attributes for subsequent management and analysis

Dynamic memory management and related topics

Qunar travel massive indicator data collection and storage
随机推荐
MVC three-tier architecture
第6节:cmake语法介绍
大疆智图、CC生产了多份数据,如何合并为一份在图新地球进行加载
力扣152题:乘积最大子数组
第1章 拦截器入门及使用技巧
What are the use cases in the Internet of things industry in 2022?
华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析
Baidu website Collection
1. Configuration environment and project creation
Disk expansion process and problems encountered in the virtual machine created by VMWare
[interview: concurrency 26: multithreading: two-phase termination mode] volatile version
Push to origin/master was rejected error resolution
会议OA项目排座功能以及送审功能
2022.7.26-----leetcode.1206
Tensorflow2.0 深度学习运行代码简单教程
Upload files to the server
[step by step, even thousands of miles] key words in the specified time period of the statistical log
证券公司哪家佣金最低?网上开户安全吗
How to use data pipeline to realize test modernization
Part II - C language improvement_ 10. Function pointer and callback function