Mini Program WXSS How to use wx:key? When the items in the list change dynamically, we need to set wx:key. Now you can provide attr "wx:key" for a "wx:for" to improve performance.
When looping an array, sometimes the following prompt will appear.
VM1364:2 ./index/index.wxml (anonymous) @ VM1364:2 VM1364:3 Now you can provide attr "wx:key" for a "wx:for" to improve performance. > 1 | <view wx:for="{{data}}" class="block" style="{{item.style}}"> | ^ 2 | Block{{index}} 3 | <view>{{item.title}}</view> 4 | </view> (anonymous) @ VM1364:3
Official explanation of wx:key:
If the position of the items in the list will change dynamically or new items are added to the list, and you want the list to be The project maintains its own characteristics and status (such as the input content in <input>
, the selected status of <switch></switch>
), you need to use wx :key
to specify a unique identifier for an item in the list.
When the items in the list change dynamically, we need to set wx:key. If we do not set it, the situation as shown above will appear. We want to add data 4. In the picture on the left, data 4 is arranged in a chaotic position. This is what we don’t want, so in order to prevent this situation, we set wx:key.
wx:key The value of
is provided in two forms:
1. String, representing a certain property of the item in the array of the for loop. The value of the property needs to be the only string or number in the list. and cannot be changed dynamically.
2. The reserved keyword this represents the item itself in the for loop. This representation requires the item itself to be a unique string or number, such as:
When data changes trigger the rendering layer to re-render, components with keys will be corrected, and the framework will ensure that they are reordered rather than recreated to ensure that the components maintain their own state and improve the efficiency of list rendering.
<switch wx:for="{{objectArray}}" wx:key="unique" > {{item.id}} </switch> <button bindtap="switch"> Switch </button> <button bindtap="addToFront"> Add to the front </button> <switch wx:for="{{numberArray}}" wx:key="*this" > {{item}} </switch> <button bindtap="addNumberToFront"> Add to the front </button>
Page({ data: { objectArray: [ {id: 5, unique: 'unique_5'}, {id: 4, unique: 'unique_4'}, {id: 3, unique: 'unique_3'}, {id: 2, unique: 'unique_2'}, {id: 1, unique: 'unique_1'}, {id: 0, unique: 'unique_0'}, ], numberArray: [1, 2, 3, 4] }, switch: function(e) { const length = this.data.objectArray.length for (let i = 0; i < length; ++i) { const x = Math.floor(Math.random() * length) const y = Math.floor(Math.random() * length) const temp = this.data.objectArray[x] this.data.objectArray[x] = this.data.objectArray[y] this.data.objectArray[y] = temp } this.setData({ objectArray: this.data.objectArray }) }, addToFront: function(e) { const length = this.data.objectArray.length this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray) this.setData({ objectArray: this.data.objectArray }) }, addNumberToFront: function(e){ this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray) this.setData({ numberArray: this.data.numberArray }) } })
Related recommendations:
Detailed explanation of wx:key for mini program development
WeChat mini program wx:key detailed introduction
The above is the detailed content of Mini program WXSS wx:key function and usage examples. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment