Improved Conditional Directives (lwc:if vs if:true ) LWC salesforce spring'23 release
Use the Improved Conditional Directives
The lwc:if, lwc:elseif, and lwc:else conditional directives supersede the legacy if:true and if:else directives.
Where: This change applies to custom Lightning web components in Lightning Experience, Experience Builder sites, and all versions of the Salesforce mobile app. This change also applies to Lightning web components in Open Source.
How: With the lwc:if, lwc:elseif, and lwc:else conditional directives, the property getters are accessed only one time per instance of the directive.
Both lwc:elseif and lwc:else must be immediately preceded by a sibling lwc:if or lwc:elseif.
Pass in an expression to lwc:if and lwc:elseif, but not to lwc:else.
If expression1 returns a truthy, none of the other property getters are accessed.
Previously, chaining if:true and if:false blocks introduced performance cost and resulted in the property getters getting called multiple times.
if:true and if:else directives are no longer recommended as we intend to deprecate and remove these directives in the future. We recommend that you replace their usage with the new conditional directives to future-proof your code.Consider these guidelines when working with the lwc:if, lwc:elseif, and lwc:else conditional directives.
- Use the conditional directives on nested
<template>tags,<div>tags, or other HTML elements, and on your custom components tags like<c-custom-cmp>. - You can’t precede
lwc:elseiforlwc:elsewith text or another element. Whitespace is ignored between the tags when the whitespace is a sibling of the conditional directive. For example, you can’t have a<div>tag that comes afterlwc:ifand beforelwc:else. - Complex expressions like
!conditionorobject?.property?.conditionaren’t supported. To evaluate complex expressions, use a getter in your JavaScript class.

Comments
Post a Comment