Sub topic 12
Migrating from if:true and if:false
This chapter assumes you can read an LWC template and JavaScript class, bind properties with curly braces, and write getters. You should also know how lwc:if and lwc:else work from earlier chapters.
Why migrate
Older LWC templates use if:true and if:false. Both still work in many orgs, but they are the legacy form. The newer directives lwc:if, lwc:elseif, and lwc:else replace them. They let you write one linked block, and they add an lwc:elseif branch that the legacy pair never had.
The legacy pair has two weaknesses:
- Each directive tests a property on its own.
if:true={isVisible}andif:false={isVisible}are two separate checks that happen to share a property. - There is no else-if. A three-way choice needs extra getters so that the conditions never overlap.
The direct translation
The legacy pair tests the same property twice. The new form tests it once and links the branches.
<template>
<!-- Legacy form -->
<template if:true={isVisible}>
<p>Visible</p>
</template>
<template if:false={isVisible}>
<p>Hidden</p>
</template>
<!-- Equivalent with lwc:if and lwc:else -->
<template lwc:if={isVisible}>
<p>Visible</p>
</template>
<template lwc:else>
<p>Hidden</p>
</template>
</template>Follow these steps for each pair:
- Replace
if:true={x}withlwc:if={x}. - Replace the matching
if:false={x}withlwc:else. Remove the={x}part, becauselwc:elsetakes no value. - Make sure the
lwc:elseblock is the next sibling after thelwc:ifblock, with nothing between them except whitespace.
Replacing negated conditions
An if:false on its own, with no matching if:true, has no direct lwc:else equivalent. An lwc:else must follow an lwc:if or lwc:elseif. Without that partner, add a getter that returns the opposite value and use lwc:if.
import { LightningElement } from 'lwc';
export default class Banner extends LightningElement {
hasItems = false;
get isEmpty() {
return !this.hasItems;
}
}<template>
<p lwc:if={isEmpty}>Nothing to show yet.</p>
</template>Collapsing extra getters into a chain
Legacy templates often needed several getters to avoid overlapping conditions. With a chain you only need a getter for each branch except the last.
<template>
<template lwc:if={isLoading}>
<p>Loading...</p>
</template>
<template lwc:elseif={hasError}>
<p>Something went wrong.</p>
</template>
<template lwc:else>
<p>Data is ready.</p>
</template>
</template>In the legacy form, the ready block needed its own getter, such as isReady, that returned !isLoading && !hasError. If you later added a new state, you had to remember to update that getter. With lwc:else, the final branch is simply "everything else". After you migrate, you can delete the getters that existed only to express "none of the above".
Migration pitfalls
| Mistake | Result | Fix |
|---|---|---|
| Leaving a value on `lwc:else`, such as `lwc:else={x}` | The template fails to compile | Write `lwc:else` with no value |
| Putting an element between the `lwc:if` and `lwc:else` blocks | The chain is broken and compilation fails | Keep the branches adjacent siblings |
| Mixing `if:true` with `lwc:else` in one chain | The pair does not link | Convert every branch in the chain |
| Writing `lwc:if={!isVisible}` | The expression is rejected | Use a getter that returns the negated value |
> Do not use block quotes in your own templates for notes. Use HTML comments instead.
Exercise
A template contains <template if:true={isOpen}>Open</template> followed by <template if:false={isOpen}>Closed</template>. Rewrite it with the new directives. Then say what you would do if a third state, isPending, must be shown before either.
Show answer
Use <template lwc:if={isOpen}>Open</template> followed immediately by <template lwc:else>Closed</template>, with no value on lwc:else. For the third state, put lwc:if={isPending} first, then lwc:elseif={isOpen} with the Open content, then lwc:else with the Closed content. You need a getter or field for isPending only, and no getter for the closed case.