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
Detailed example of IOS database upgrade data mig...
I don’t know if you have ever encountered such a ...
This article mainly discusses the differences bet...
1. Application Scenarios Parent page a.jsp Subpage...
After minimizing the installation of Python8, I i...
Table of contents How to set cookies Disadvantage...
50 lines of code to change 5 skin colors, includi...
Preface Docker has been very popular in the past ...
Table of contents The background is: What will ha...
Method 1: Use Google advanced search, for example...
To install Jenkins on CentOS 8, you need to use t...
When the front-end requests the interface, it is ...
Compatible with new CSS3 properties In CSS3, we c...
Let's take a look at the detailed method of b...
First create a specific project directory for you...