search
HomeWeChat AppletMini Program DevelopmentSuggestions on using the hidden attribute of components in WeChat mini programs

1, let’s first look at an author’s experiment

It is mentioned in WeChat’s official documentation that hidden is a property that all components have. In actual coding, it was found that its performance is similar to The description doesn't quite fit the description.

For example, the following layout:

You will find that hidden does not take effect.

After experiments, it was found that the hidden element only takes effect on block layout, so the culprit in this code that causes hidden to not take effect is display:flex. Just remove this.

In order to verify what the author said, I turned over the css document and practiced all possible values ​​of the display style one by one:

Value Description

none This element will not be displayed .

block This element will be displayed as a block-level element, with line breaks before and after this element.

inline Default. This element will be displayed as an inline element with no line breaks before or after the element.

inline-block Inline block element. (New value in CSS2.1)

list-item This element will be displayed as a list.

run-in This element will be displayed as a block-level element or an inline element depending on the context.

compact There is a value compact in CSS, but it has been removed from CSS2.1 due to lack of widespread support.

marker There is a value marker in CSS, but it has been removed from CSS2.1 due to lack of widespread support.

table This element will be displayed as a block-level table (similar to ), with line breaks before and after the table.

inline-table This element will be displayed as an inline table (similar to ), with no line breaks before and after the table.

table-row-group This element will be displayed as a group of one or more rows (similar to ).

table-header-group This element will be displayed as a group of one or more rows (similar to ).

table-footer-group This element will be displayed as a group of one or more rows (similar to ).

table-row This element will be displayed as a table row (similar to ).

table-column-group This element will be displayed as a group of one or more columns (similar to ).

table-column This element will be displayed as a cell column (similar to)

table-cell This element will be displayed as a table cell (similar to and)

table -caption This element will be displayed as a table title (similar)

inherit Specifies that the value of the display attribute should be inherited from the parent element.

Changing to any value, except none, as well as the abandoned compact and marker, will not work.

So, WHY?

Let’s continue reading the original text first~

2, use display:none to control the display and concealment

What if you must use flex layout?

In fact, the purpose of using hidden here is just to hide this layout. Display:none can also hide it. Here you can use a tricky method to dynamically set the display attribute. The example is as follows:

The hideview here is a variable in the corresponding js and is dynamically controlled by js.

Afterword: hidden hides the layout. Although it is hidden, it still takes up space. display:none hides and does not occupy space.

3. Regarding the difference between wx:if and hidden

, how should we understand the last sentence? Doesn’t it take up interface space?

no!

What the author wants to express, it is speculated that although hidden hides the view component, the component will still be rendered; display:none and hidden=true have the same effect, and display:none will still render the component.

If you want not to render unnecessary components, use conditional rendering: wx:if

wx:if vs hidden

Because the template in wx:if may also Contains data binding, so when the conditional value of wx:if switches, the framework has a local rendering process, because it will ensure that the conditional block is destroyed or re-rendered when switching.

At the same time, wx:if is also lazy. If the initial rendering condition is false, the framework does nothing and starts partial rendering when the condition becomes true for the first time. In contrast, hidden is much simpler. The component will always be rendered, and it is just a simple control of display and hiding. Generally speaking, wx:if has a higher switching cost and hidden has a higher initial rendering cost. Therefore, if frequent switching is required, hidden is better, and wx:if is better if conditions are unlikely to change at runtime. (Reposted from the WeChat document)

4, Regarding the answer to the WHY above

Back to the question of the red statement above, it cannot be said that the problem is caused by display:flex. It's caused by the programmers themselves.

Look at the rendering of view style:

Suggestions on using the hidden attribute of components in WeChat mini programs

The applet first renders the component name style (i.e. view), and then renders the style attribute style. (Not tested on ios and android phones)

So it is easy to understand. It is not that the hidden attribute of the WeChat applet does not take effect, but the hidden attribute of the applet is achieved by adding a display:none to the name style, see the red arrow above.

Then, the programmer set a display:block in the style attribute, directly overriding the settings of the mini program. So, how could it possibly work? Isn’t this the fault of the programmers themselves?

So, why is it easier to use the obsolete compact and marker?

Because these two do not exist in the mini program at all. Setting them equals no setting.

5. Regarding the value of the hidden attribute, it doesn’t matter if you don’t set it. Setting it is useless.

In addition, there is one thing about the value of the hidden attribute, like the above code:

hidden="true"

In fact, setting it to hidden="false", or "0", or any other value is equivalent, and the result is true.

This is not only true for the hidden attribute, but also for all Boolean attributes in the applet component. The completion can be used like this:

Just write this hidden attribute.

6. It is recommended not to use the hidden attribute. It is a useless and stupid attribute.

So, everyone has seen it:

1. Use display :none can achieve the same effect as hidden

2. No matter how the value of hidden is set, the result is true. You don’t even have to do dynamic binding. You can only use wx:if for conditional rendering

3, having said that, if we have to use lazy wx:if conditional rendering, why should I use hidden?

wx: if rendering is lazy rendering, it only renders when needed. The disadvantage is that it is repeatedly destroyed and rebuilt, which consumes electricity! (It’s a waste of mobile phone CPU and really wastes electricity)

And display:none just switches the display, and the things that have been rendered are still there.

To summarize, if it is not a long list rendering, it is recommended to use display:none to control the display and hiding. If it is a long list rendering, use conditional rendering.

Hidden is a completely stupid attribute with no use. It will only confuse programmers.

The above is the detailed content of Suggestions on using the hidden attribute of components in WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
微信小程序架构原理基础详解微信小程序架构原理基础详解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

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

简单介绍:实现小程序授权登录功能简单介绍:实现小程序授权登录功能Nov 07, 2022 pm 05:32 PM

本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了怎么实现小程序授权登录功能的相关内容,下面一起来看一下,希望对大家有帮助。

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
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft