Detailed explanation of the principle and function of Vue list rendering key

Detailed explanation of the principle and function of Vue list rendering key

The principle and function of list rendering key

The key is used to identify the node. If the key is bound to the value of the index, problems may easily occur:

1. If the data is added or deleted in reverse order, unnecessary real DOM updates will occur. The page effect is fine, but the efficiency is 2 problems.

2. If the deconstruction also includes the DOM of the input class, an incorrect DOM update will occur - there is a problem with the interface

Case analysis of the problem:

Click the button to add an object to the front of the list

    <div id="root">
        <button @click.once="add">Add a Lao Liu</button>
        <ul>
            <li v-for="(p, index) in persons" :key="index">
                {{p.name}}---{{p.age}}
                <input type="text">
            </li>
        </ul>
    </div>
        var vm = new Vue({
            el:"#root",
            data:{
                persons:
                    {id:"001", name:"张三", age:15},
                    {id:"002", name:"Li Si", age:16},
                    {id:"003", name:"王五", age:17}
                ]
            },
            methods: {
                add(){
                    const p = {id:"004", name:"老刘", age:20}
                    this.persons.unshift(p)
                }
            },
        })

This is the effect shown

When we fill in the name in the input box

insert image description here

Then click the button and observe the problem

insert image description here

We will find that Lao Liu appears at the top of the form, but the content in the list is not equal to the content in the input box.

Solution: We key="index"改為:key="id"

Analysis of the key principle

insert image description here

Initial data

Get the initial data and generate a virtual DOM based on the initial data. Convert the virtual DOM to a real DOM

New data

Get the new data (including Lao Liu) and generate a virtual DOM based on the data. Compare the virtual DOM with the virtual DOM of the initial data (the text information in li is different, but the input is the same. The virtual DOM has no data. The content entered on the page is stored in the real DOM) Text information - the new data replaces the initial data, and the input content is retained

The role of key

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • A detailed explanation of Vue.js list rendering that you must know
  • Detailed explanation of what role key plays in Vue list rendering
  • Detailed explanation of vuejs v-for list rendering
  • Vue.js learning tutorial list rendering detailed explanation
  • Detailed explanation of the first introductory tutorial of Vuejs (one-way binding, two-way binding, list rendering, response function)
  • Detailed explanation of Vue's list rendering

<<:  Detailed explanation of the use of CSS pointer-events attribute

>>:  How to configure MySQL scheduled tasks (EVENT events) in detail

Recommend

WeChat Mini Program Basic Tutorial: Use of Echart

Preface Let’s take a look at the final effect fir...

vue.js Router nested routes

Preface: Sometimes in a route, the main part is t...

HTML table tag tutorial (19): row tag

The attributes of the <TR> tag are used to ...

Complete steps for using Echarts and sub-packaging in WeChat Mini Program

Preface Although the holiday is over, it shows up...

js realizes two-way data binding (accessor monitoring)

This article example shares the specific code of ...

JavaScript to achieve click image flip effect

I was recently working on a project about face co...

Detailed explanation of the pitfalls of mixing MySQL order by and limit

In MySQL, we often use order by for sorting and l...

Detailed explanation of CSS sticky positioning position: sticky problem pit

Preface: position:sticky is a new attribute of CS...

JavaScript recursion detailed

Table of contents 1. What is recursion? 2. Solve ...

CentOS 6 Compile and install ZLMediaKit analysis

Install ZLMediaKit on centos6 The author of ZLMed...

Practical method of deleting files from Linux command line

rm Command The rm command is a command that most ...

Javascript Basics: Detailed Explanation of Operators and Flow Control

Table of contents 1. Operator 1.1 Arithmetic oper...