Sub topic 14
Removing from the DOM versus hiding with CSS
This chapter assumes you can write an LWC with a template and a JavaScript class, bind properties with curly braces, and use lwc:if and lwc:else. It compares two ways to make content disappear and gives you criteria for choosing between them.
Two ways to hide content
lwc:if removes content from the DOM. When the condition is false, the elements do not exist. The other approach keeps the elements in the DOM and hides them with a CSS class, usually slds-hide or a custom rule that sets display: none.
<template>
<!-- Removed from the DOM when false -->
<p lwc:if={showDetails}>Details are visible.</p>
<!-- Always in the DOM, hidden by CSS when false -->
<p class={detailsClass}>Details are visible.</p>
</template>import { LightningElement } from 'lwc';
export default class DetailsToggle extends LightningElement {
showDetails = false;
get detailsClass() {
return this.showDetails ? '' : 'slds-hide';
}
handleToggle() {
this.showDetails = !this.showDetails;
}
}Both paragraphs look the same to the user. The difference is what the browser and the framework do behind the scenes.
What differs
| Concern | `lwc:if` (removed) | CSS hiding |
|---|---|---|
| DOM nodes | Created and destroyed on each toggle | Created once, kept |
| Child component lifecycle | `constructor` and `connectedCallback` on show, `disconnectedCallback` on hide | Run once, not tied to visibility |
| Child component state | Lost when removed | Preserved |
| Initial render cost | Skipped while false | Paid up front, even if never shown |
| Toggle cost | Re-creates the subtree | Changes one class |
| Query access | `this.template.querySelector` returns `null` while removed | Element is always found |
| Screen readers | Not exposed while removed | `display: none` also hides it, but `visibility` or off-screen tricks may not |
Choosing between them
Prefer lwc:if when:
- The content is expensive and often not needed, such as a data table, a chart, or a component that makes a server call on load.
- You want a child component to start fresh each time it appears.
- The content should not exist at all for some states, such as an error message.
- The branches are mutually exclusive, as in a loading, error, and ready chain.
Prefer CSS hiding when:
- The user toggles the content often, and re-creating it would cause a visible delay.
- You must keep local state, such as text typed into a form field or a scroll position.
- The child component is costly to initialize and you want to pay that cost once.
- You need to measure or query the element while it is hidden.
A common trap with CSS hiding
A hidden component is still alive. If it loads data in connectedCallback, that call runs on first render, even if the user never opens the section. Every hidden panel in a tabbed layout that uses CSS hiding loads its data immediately. With lwc:if, only the active tab runs its setup.
A common trap with `lwc:if`
Because the subtree is destroyed, any value the user entered inside it is lost when the branch toggles off. If a user types into an input inside an lwc:if block and the block closes, reopening shows an empty input. Store the value in a property on the parent if it must survive.
Exercise
A tabbed panel has three tabs. Each tab body is a child component that calls an Apex method in connectedCallback. Users switch tabs often and do not want to lose what they typed in each tab. Which approach fits, and what cost do you accept?
Show answer
Use CSS hiding for the inactive tabs. The child components stay alive, so their typed input is preserved and switching is a cheap class change. The cost is that all three components are created and all three Apex calls run when the panel first renders, even for tabs the user never opens. Alternatively, use lwc:if and keep the typed values in the parent's properties, which defers the Apex calls but requires extra code.
Exercise
You use lwc:if={showForm} around a child component and call this.template.querySelector('c-my-form') in a click handler while showForm is false. What does the call return, and why?
Show answer
It returns null. When the condition is false, the child component is not in the DOM, so the query finds nothing.