A brief discussion on the role and working principle of key in Vue3

A brief discussion on the role and working principle of key in Vue3

What is the function of this key attribute? Let’s first look at the official explanation:

  • The kekey attribute is mainly used in Vue's virtual DOM diff algorithm to identify Vnodes when comparing new and old nodes;
  • If you don't use keys, Vue will use an algorithm that minimizes dynamic elements and tries to modify/reuse elements of the same type in place as much as possible.
  • When using a key, it will rearrange the order of elements based on the change of the key, and will remove/destroy the elements that do not exist in the key.

Let me briefly mention my understanding of VNode:

  • The full name of VNode is Virtual Node, which is a virtual node;
  • The essence of Vnode is a JavaScript object;
  • In fact, in Vue, both components and individual elements are represented as VNodes.

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:

  • Re-render once (consumes a lot of performance)
  • The previous VNode remains unchanged and is re-rendered from the insertion position (consuming a large amount of performance)
  • All VNnodes are re-rendered, only the newly added VNodes need to be inserted (best performance)

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:
  • Detailed explanation of what role key plays in Vue list rendering
  • Detailed explanation of the role and principle of key in Vue
  • Detailed explanation of the principle and function of Vue list rendering key

<<:  Two ways to manually implement MySQL dual-machine hot standby on Alibaba Cloud Server

>>:  How to use Linux tr command

Recommend

Detailed example of IOS database upgrade data migration

Detailed example of IOS database upgrade data mig...

Analysis of the process of building a LAN server based on http.server

I don’t know if you have ever encountered such a ...

Centos8 (minimum installation) tutorial on how to install Python3.8+pip

After minimizing the installation of Python8, I i...

Vue keeps the user logged in (various token storage methods)

Table of contents How to set cookies Disadvantage...

Implementation code of front-end HTML skin changing function

50 lines of code to change 5 skin colors, includi...

What are the drawbacks of deploying the database in a Docker container?

Preface Docker has been very popular in the past ...

js implements axios limit request queue

Table of contents The background is: What will ha...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

How to install Jenkins on CentOS 8

To install Jenkins on CentOS 8, you need to use t...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

Detailed explanation of commonly used CSS styles (layout)

Compatible with new CSS3 properties In CSS3, we c...

Detailed process of building mongodb and mysql with docker-compose

Let's take a look at the detailed method of b...

How to run Python script on Docker

First create a specific project directory for you...