What is the function of this key attribute? Let’s first look at the official explanation:
Let me briefly mention my understanding of VNode:
For example: <div class="title" style="color: red;">Helllo World</div> // Actually in Vue it will be represented as: const VNode = { type: "div", props: { class: "title", style: { color: "red" } }, children: "Hello World" // If the div contains other tags, they will be converted to children in the same way} Under what circumstances is the insertion of f most efficient? Ideas:
Conclusion: If you want the highest performance, you must key the new and old VNodes, compare the old VNodes with the new VNodes by key value, and find out which VNode needs to be added or deleted. Other VNodes should remain unchanged as much as possible. The process of comparing the new and old VNodes is the diff algorithm. Vue actually calls two different methods for keys and no keys. Let's take a look at the source code: (packages\runtime-core\src\renderer) In the case of a key, execute the patchKeyedChildren method: If there is no key value, execute the patchUnkeyedChildren method: Note: When there is no key value, if there are too many lists, patches will be performed one by one, which will consume a lot of performance. Then adding the key value will save a lot of performance loss. in conclusion: Therefore, the key value plays a role when the DOM tree performs the diff algorithm. One function is to determine whether the new and old Vnodes are the same, so as to perform the next step of comparison and rendering. The other function is to determine whether the component can be reused and whether it needs to be re-rendered. This is the end of this article about the role and working principle of key in Vue3. For more information about the role and working principle of key in Vue3, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Two ways to manually implement MySQL dual-machine hot standby on Alibaba Cloud Server
>>: How to use Linux tr command
Nginx uses a fixed number of multi-process models...
1. Introduction: If we want to display flash conte...
Table of contents Preface Introduction to Dockerf...
HTML Design Pattern Study Notes This week I mainl...
Postfix is a free and open source MTA (Mail Tra...
Pop-up windows are often used in actual developme...
Currently, almost all large websites and applicat...
1. Change password 1. Modify the password of ordi...
In this article, the blogger will take you to lea...
This article shares the specific code of Bootstra...
<br />I have been working in front-end for s...
Today, I logged into the server and prepared to m...
Experimental environment: Physical machine Window...
This article shares the installation and configur...
Table of contents CentOS rpm installation and con...