search
HomeWeChat AppletMini Program DevelopmentThe role of wx:key in list rendering and the difference between conditional rendering wx:if and hidden

The content of this article is about the method of using async/await syntax (code example) in WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Developing WeChat mini programs is inseparable from "page rendering". It is difficult for beginners to understand what "page rendering" in mini programs is and how to use it?
For students who have learned vue, this is more familiar. It is actually data binding page rendering.
So the most important things about page rendering are list rendering and conditional rendering. Let’s take a look at a few simple examples first.

The following is an example of "List rendering":

<view>
  {{index}}: {{item.message}}
</view>
Page({
  data: {
    array: [{
      message: 'foo',
    }, {
      message: 'bar'
    }]
  }
})

As can be seen from the above example, the subscript variable name of the current item of the default array defaults to index, and the array The variable name of the current item defaults to item. Of course, you can use wx:for-item to specify the variable name of the current element of the array, and use wx:for-index to specify the variable name of the current subscript of the array, as follows:

<view>
  {{idx}}: {{itemName.message}}
</view>

The following is a "Conditional rendering" Example:

<view>True</view>
Page({
  data: {
    condition: true
  }
})

The above example shows that when condition is true, the page renders the above view tag. Of course, you can also use wx:elif and wx:else to add an else block, as follows:

<view> 5}}">1</view>
<view> 2}}">2</view>
<view>3</view>

Let’s go to the main topic and explore the questions about the article title

1. What is the role of wx:key in list rendering?

In fact, you may be a little confused about wx:key when you read the official documents for the first time. The official explanation is this:

The role of wx:key in list rendering and the difference between conditional rendering wx:if and hidden

According to my many years of experience in reading documents, I can generally ignore unimportant text that I can’t understand and just focus on the key points, such as the bold part of the text in the picture above. Therefore, I chose not to write wx at the beginning. :key this attribute. However, after writing too many list renderings (without adding wx:key) during the development process, the console will report a lot of wx:key warnings. I am a bit of a code addict, which seems very uncomfortable, but I also suffer from not knowing wx:key. The real role of key, so I created a solution, which is to add wx:key="{{index}}" after each list rendering, similar to the following:

<view>
  {{item}}
</view>

So I was surprised I found that all the warnings were gone and there were no other negative effects, so I used it like this for more than half a year.
However, in a project I worked on half a year ago, there was a list rendering that needed to try to obtain the user's avatar and nickname, so my previous method did not work. The user information obtained each time did not correspond to the current content, and would Confusion occurs. So I re-understood wx:key, and combined with the following example, I seemed to understand:

<switch>
  {{item.id}}
</switch>
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'},
    ]
  }
})

In fact, wx:key is used to bind the characteristics of the items in the current list, that is, if The list is dynamically updated, so the role of wx:key is to keep the entire state of the original project unchanged.
Combined with the above example, we can know that if the list array is an object array, then the wx:key attribute can directly write the corresponding unique attribute name, such as the above wx:key="unique", or wx: key="id" is also possible, as long as the attribute is a unique value, it is similar to the id attribute in the page tag that is unique on the page.
For the list array is a basic type array, then just write wx:key="*this" directly, as follows:

<block>
  <view>{{index}}:</view>
  <view>{{item}}</view>
</block>

Use the wx:key attribute skillfully

  • If it is clear that your list rendering is a static list, then you can do what I did at the beginning and add wx:key="{{index}}"

  • On the contrary, if it is a dynamic list, then you have to find the unique key value in the array and put it in wx:key

  • Of course, if you ignore the warning, it will not affect the function. It’s okay if you don’t add it

2. What’s the difference between wx:if and hidden

In fact, when we use conditional rendering, we mostly use wx:if instead of hidden, because The former can be expanded, but the latter lacks certain logic. But what is the difference between them?
The official document describes it like this:

The role of wx:key in list rendering and the difference between conditional rendering wx:if and hidden

In the above picture, we can probably understand that if you need to switch states frequently, use hidden, otherwise use wx:if.
In other words, wx:if can create or destroy rendering components in real time, and it will be created when it is true. It will do nothing when it is initially false, and it will be destroyed when it changes from true to false. So switching it frequently is a relatively performance-intensive move. And hidden means that the component will be rendered on the page when the page is initially rendered. The true or false value only controls its display and hiding. If the page is not destroyed, the component will not be destroyed either.
Understand this, you will find that from the perspective of our developers, flexible use of these two conditional judgments will get twice the result with half the effort.
Here are several usage scenarios for developers’ reference:

<view>加载中……</view>
<view>没有更多了</view>

上面代码是一个上拉加载动画显示与隐藏组件,可以看到用的是 hidden,因为他是一个需要频繁切换的组件。

<block>
  <view>
    <text>{{node.children[0].text}}</text>
  </view>
</block>
<block>
  <image></image>
</block>
<block>
  <video></video>
</block>

上面代码展示的是渲染文字还是图片或者是视频,只展示其中的一个那么用 wx:if 最佳。

下面是一个自定义 input 组件:

<view>
  <view>
    <input>
    <view>发送</view>
  </view>
</view>

其功能是点击评论按钮能实时显示输入框,否则隐藏。这里为什么用 wx:if 呢?因为我希望它显示时是新的 input 组件,不是之前渲染好的,这样如果我刚输入完文字,再次评论不会出现上一次的文字残留。

巧用 wx:if 和 hidden

  • 有时我们需要提前渲染好里面的子组件,那么要用 hidden,否则待显示时需要加上渲染的时间

  • 通常情况下,我在隐藏的时候都不需要该组件的话,那就用 wx:if

  • 如果需要在页面中点击切换的渲染,那么考虑小程序性能问题,还是用 hidden 为好

三、思考(引伸)

1、 这个元素在列表和条件渲染上是很好用的,不过要注意不要在这个标签上绑定其他属性,比如 data- 或者绑定事件 bindtap。下面是一个反例:

<block>
  <view>view1</view>
  <view>view2</view>
</block>

上面的代码里,在 js 中定义绑定事件后,你会发现不会执行。原因就在 元素在渲染页面后并不会存在,他不是个组件,不会渲染在页面树节点里面,所以在他上面绑定的事件或者属性也不会有效。

2、 当 wx:for 的值为字符串时,会将字符串解析成字符串数组;另外,花括号和引号之间如果有空格,将最终被解析成为字符串,请看下面的例子:

<view>
  {{item}}
</view>

等同于

<view>
  {{item}}
</view>
<view>
  {{item}}
</view>

等同于

<view>
  {{item}}
</view>

本文参考:微信小程序开发基础教程 https://www.html.cn/study/20.html

The above is the detailed content of The role of wx:key in list rendering and the difference between conditional rendering wx:if and hidden. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
微信小程序架构原理基础详解微信小程序架构原理基础详解Oct 11, 2022 pm 02:13 PM

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了关于基础架构原理的相关内容,其中包括了宿主环境、执行环境、小程序整体架构、运行机制、更新机制、数据通信机制等等内容,下面一起来看一下,希望对大家有帮助。

微信小程序云服务配置详解微信小程序云服务配置详解May 27, 2022 am 11:53 AM

本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了关于云服务的配置详解,包括了创建使用云开发项目、搭建云环境、测试云服务等等内容,下面一起来看一下,希望对大家有帮助。

微信小程序常用API(总结分享)微信小程序常用API(总结分享)Dec 01, 2022 pm 04:08 PM

本篇文章给大家带来了关于微信小程序的相关知识,其中主要总结了一些常用的API,下面一起来看一下,希望对大家有帮助。

浅析微信小程序中自定义组件的方法浅析微信小程序中自定义组件的方法Mar 25, 2022 am 11:33 AM

微信小程序中怎么自定义组件?下面本篇文章给大家介绍一下微信小程序中自定义组件的方法,希望对大家有所帮助!

微信小程序实战项目之富文本编辑器实现微信小程序实战项目之富文本编辑器实现Oct 08, 2022 pm 05:51 PM

本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了关于富文本编辑器的实战示例,包括了创建发布页面、实现基本布局、实现编辑区操作栏的功能等内容,下面一起来看一下,希望对大家有帮助。

西安坐地铁用什么小程序西安坐地铁用什么小程序Nov 17, 2022 am 11:37 AM

西安坐地铁用的小程序为“乘车码”。使用方法:1、打开手机微信客户端,点击“发现”中的“小程序”;2、在搜索栏中输入“乘车码”进行搜索;3、直接定位城市西安,或者搜索西安,点击“西安地铁乘车码”选项的“去乘车”按钮;4、根据腾讯官方提示进行授权,开通“乘车码”业务即可利用该小程序提供的二维码来支付乘车了。

微信小程序开发工具介绍微信小程序开发工具介绍Oct 08, 2022 pm 04:47 PM

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了关于开发工具介绍的相关内容,包括了下载开发工具以及编辑器总结等内容,下面一起来看一下,希望对大家有帮助。

订阅号跟小程序的区别是什么订阅号跟小程序的区别是什么Aug 25, 2022 pm 04:54 PM

订阅号跟小程序的区别:1、小程序加载小组件较慢,有专门提供的组件,而订阅号加载组件较快,但是时原生的页面组件;2、小程序的运营后台是实时数据,而订阅号则是非实时数据;3、小程序与聊天窗口之间可以相互切换,而订阅号则需要先退出才能进入聊天窗口;4、小程序的接口数量较多,而订阅号的接口数量较少,受限于浏览器,只提供有限的功能。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!