Sub topic 15
Common mistakes and how to avoid them
This chapter assumes you can write an LWC template and class, bind properties with curly braces, and write JavaScript getters. It collects the errors that come up most often with lwc:if, lwc:elseif, and lwc:else. It also shows how to fix each one.
Putting an expression in the directive
The value of lwc:if and lwc:elseif must be a plain property reference. LWC templates do not evaluate JavaScript expressions, so comparisons, negation, and function calls fail to compile.
<template>
<!-- Invalid: expressions are not allowed -->
<template lwc:if={items.length > 0}>
<p>Items found</p>
</template>
<template lwc:if={!isLoading}>
<p>Done</p>
</template>
</template>Move the logic into getters and bind to those.
import { LightningElement } from 'lwc';
export default class ItemList extends LightningElement {
items = [];
isLoading = false;
get hasItems() {
return this.items.length > 0;
}
get isLoaded() {
return !this.isLoading;
}
}<template>
<template lwc:if={hasItems}>
<p>Items found</p>
</template>
<template lwc:else>
<p>No items</p>
</template>
</template>Giving lwc:else a value
lwc:else is a bare attribute. Writing lwc:else={something} is a compile error, because lwc:else has no condition. It runs when every earlier branch in the chain was false.
Breaking the chain
lwc:elseif and lwc:else must be siblings that come directly after the lwc:if or lwc:elseif they continue. Any other element between them ends the chain, and the compiler then reports the lwc:else as having no matching lwc:if.
| Mistake | Result | Fix |
|---|---|---|
| `lwc:else` with no preceding `lwc:if` | Compile error | Add the `lwc:if` first |
| An element between `lwc:if` and `lwc:else` | Compile error | Move the element outside the chain |
| `lwc:else` after `if:true` | Compile error | Use `lwc:if` for the whole chain |
| `lwc:if` and `if:true` on one element | Compile error | Pick one directive |
Do not mix the legacy if:true and if:false directives with the newer ones. Migrate a whole block at once.
Misreading truthiness
The directive renders a branch when the value is truthy. These values are falsy: false, 0, "", null, undefined, and NaN. Everything else is truthy, and that includes two values that surprise people.
- An empty array
[]is truthy, solwc:if={items}renders even when the list is empty. - The string
"false"is truthy.
Bind to a getter that returns a real boolean, such as hasItems above. This also makes the intent clear to the next reader.
Expecting hidden content to keep its state
When a branch becomes false, LWC removes its DOM. Child components in that branch are disconnected and destroyed. When the branch becomes true again, new instances are created, so their fields reset and connectedCallback runs again. If a child must keep its state while it is out of view, hide it with a CSS class instead and keep it in the DOM.
Checking the wrong thing in a getter
A getter that reads a field is re-evaluated when that field is reassigned. Compare against the exact value your code sets. A typo such as 'Loading' against 'loading' leaves every branch false, so only the lwc:else content shows. When a chain always shows the fallback, log the field in the getter and check its real value.
Exercise
This template fails to compile. Name two problems and say how to fix them. <template lwc:if={count > 0}><p>Has items</p></template> <p>Footer</p> <template lwc:else={isEmpty}><p>Empty</p></template>
Show answer
First, count > 0 is an expression. Replace it with a getter such as hasCount that returns this.count > 0. Second, lwc:else has a value and an element sits between it and the lwc:if. Remove the value from lwc:else, and move the footer paragraph before the chain or after the lwc:else block.