Sub topic 12
Migrating from if:true and if:false
This chapter assumes you can read an LWC template and its JavaScript class, and that you have used lwc:if, lwc:elseif, and lwc:else in earlier chapters. It covers how to convert templates that still use the older if:true and if:false directives, and where the two approaches behave differently.
Why migrate
if:true and if:false are the original conditional directives in LWC. Salesforce now recommends lwc:if, lwc:elseif, and lwc:else for new work. The newer directives have three advantages:
- They form one linked chain, so you can write if/else-if/else logic without repeating conditions.
- The compiler knows which branch is the fallback, so the condition is evaluated once for the whole chain.
- They read like the control flow they describe.
Existing templates with the legacy directives keep working. Migrate when you edit a template anyway, or when duplicated conditions have started to cause bugs. You do not need a bulk rewrite.
The legacy directives
The legacy directives are separate checks. Each one takes a property reference and is evaluated by itself. There is no else. To get one, you test the same property twice, once with if:true and once with if:false.
<template>
<template if:true={isVisible}>
<p>Visible</p>
</template>
<template if:false={isVisible}>
<p>Hidden</p>
</template>
</template>Both blocks look at isVisible. The compiler does not know they are related. The pairing exists only in your head, and it breaks if someone changes one condition and forgets the other.
The direct mappings
Most migrations follow one of four patterns.
| Legacy pattern | Replacement |
|---|---|
| `if:true={x}` on its own | `lwc:if={x}` |
| `if:true={x}` followed by `if:false={x}` | `lwc:if={x}` followed by `lwc:else` |
| `if:false={x}` on its own | `lwc:if={notX}`, where `notX` is a getter that returns `!this.x` |
| Several `if:true` blocks on different properties | `lwc:if` and `lwc:elseif`, only if the conditions are meant to be exclusive |
Pair on the same property
This is the easiest case. Keep the first block and change the directive. Turn the second block into lwc:else and delete its value.
<template>
<template lwc:if={isVisible}>
<p>Visible</p>
</template>
<template lwc:else>
<p>Hidden</p>
</template>
</template>lwc:else takes no value. Writing lwc:else={isVisible} is a compile error, which is a common slip when you edit the attribute by hand.
A lone if:false
lwc:if accepts only a property or getter reference. It does not accept !isReady or any other expression. A lone if:false={isReady} therefore needs a getter that carries the negation.
Before:
<template>
<template if:false={isReady}>
<p>Please wait.</p>
</template>
</template>After:
<template>
<template lwc:if={isNotReady}>
<p>Please wait.</p>
</template>
</template>import { LightningElement } from 'lwc';
export default class Waiting extends LightningElement {
isReady = false;
get isNotReady() {
return !this.isReady;
}
}If the template also has a block for the ready state, you can often avoid the negating getter. Put the ready content under lwc:if={isReady} and the waiting message under lwc:else.
Where behavior differs
The mappings above look mechanical, but the two models are not identical. Check these points before you commit a migration.
Independent blocks can both render
Legacy blocks do not know about each other. If two if:true blocks use different properties and both properties are truthy, both blocks render. A chain renders at most one branch. Converting two independent blocks into lwc:if and lwc:elseif silently changes what the user sees when both conditions hold.
<template>
<!-- Legacy: both can show at once -->
<template if:true={hasWarning}>
<p>Warning</p>
</template>
<template if:true={hasError}>
<p>Error</p>
</template>
</template>Decide what you want when both flags are true. If the answer is "show both", keep two separate lwc:if blocks. If the answer is "show the most severe one", use a chain and order the branches by priority.
<template>
<template lwc:if={hasError}>
<p>Error</p>
</template>
<template lwc:elseif={hasWarning}>
<p>Warning</p>
</template>
</template>Here a chain without a final lwc:else is fine. If neither flag is true, nothing renders.
Paired legacy blocks on different properties
Sometimes a pair looks like an if/else but uses two different properties, such as if:true={isLoading} and if:true={hasData}. These may overlap or leave gaps. Before you convert such a pair to a chain, write down every combination of the two values and decide what should render for each. Often the answer is to replace both with one status field and getters, as the worked example in an earlier chapter did.
Evaluation and ordering
In a chain, the compiler evaluates conditions from top to bottom and stops at the first truthy one. Legacy directives evaluate every block. This matters when a getter is expensive or has side effects, though getters used in templates should not have side effects. If you move from many legacy blocks to one chain, a getter lower in the chain may no longer run on every render.
Placement rules to apply during migration
The new directives are stricter about structure than the legacy ones, so some templates need rearranging:
lwc:elseifandlwc:elsemust be the next sibling element of anlwc:iforlwc:elseif. Another element between them breaks the chain and produces a compile error.- A chain can be written on
<template>tags or directly on elements, and you can use either form for each branch. - Do not put legacy and new directives on the same element. Convert a whole conditional group at once instead of mixing them.
- Each
lwc:ifstarts a new chain. Anlwc:elsepairs only with the nearest precedinglwc:iforlwc:elseif.
Legacy blocks could be separated by anything, because they were independent. If a template has something like a heading between the if:true and the if:false, move it out of the middle before converting.
<template>
<!-- Legacy: unrelated element between the pair -->
<template if:true={isAdmin}>
<p>Admin tools</p>
</template>
<hr>
<template if:false={isAdmin}>
<p>Standard view</p>
</template>
</template>To convert this, decide where the <hr> belongs. Either place it before the chain, after the chain, or inside both branches if it should appear in both cases.
<template>
<template lwc:if={isAdmin}>
<p>Admin tools</p>
</template>
<template lwc:else>
<p>Standard view</p>
</template>
<hr>
</template>A migration checklist
Work through each template in this order:
- List every
if:trueandif:falseblock, and note which properties each one reads. - Group blocks that read the same property or that are meant to be alternatives.
- For each group, pick a row from the mapping table and write the replacement.
- Add any negating getters to the JavaScript class.
- Check sibling adjacency and make sure no
lwc:elsecarries a value. - Run the component and test every combination of the underlying values, not only the common ones.
What does not change
The conversion changes how you write the conditions, not how the DOM is managed. The false branch still adds no elements, and child components in a toggled branch are still created and destroyed as before. Truthy and falsy rules are the same: 0, '', null, undefined, NaN, and false are falsy, and everything else is truthy, including an empty array or object. Getters work the same way, and an if:true that used a getter can move to lwc:if with no change to the JavaScript.
Common mistakes during migration
- Writing
lwc:else={isVisible}instead of a barelwc:else. - Writing
lwc:if={!isVisible}. Expressions are not supported, so add a getter. - Leaving an element between the
lwc:ifand itslwc:elseorlwc:elseif. - Turning independent warning and error blocks into a chain and losing the case where both show.
- Mixing
if:trueandlwc:ifon one element during a half-finished edit.
Exercise
A template contains <template if:true={hasDraft}> showing a draft banner, then a <template if:false={hasDraft}> block with a "No draft" message, and between them an <hr> that should appear only in the draft case. Rewrite the structure with the new directives, and state what you do with the <hr>.
Show answer
Use <template lwc:if={hasDraft}> and put the draft banner and the <hr> inside it. Follow that directly with <template lwc:else> containing the "No draft" message, with no value on lwc:else. The <hr> moves inside the first branch because an element between the lwc:if block and the lwc:else block would break the chain and cause a compile error.
Exercise
A component has <template if:true={hasWarning}> and <template if:true={hasError}> as independent blocks. A teammate converts them to lwc:if={hasWarning} and lwc:elseif={hasError}. What changes for the user, and how would you fix it if the intent was to show only the more severe message?
Show answer
With the chain, an error is never shown while hasWarning is true, because the first truthy branch wins and the rest are skipped. In the legacy version both messages showed when both flags were true. To show only the more severe message, order the chain with lwc:if={hasError} first and lwc:elseif={hasWarning} second. If both should show, keep two separate lwc:if blocks.