Sub topic 13
What happens to child components when a branch toggles
This chapter assumes you can write a basic LWC with lwc:if, lwc:elseif, and lwc:else, and that you know the component lifecycle hooks constructor, connectedCallback, renderedCallback, and disconnectedCallback.
Toggling creates and destroys DOM
A conditional branch does not hide its content. When the condition is falsy, the engine removes the branch's elements from the DOM. When the condition becomes truthy, the engine creates them again.
This matters most for child components. A child inside a branch is not paused or detached for later. It is destroyed when the branch goes away and built from scratch when the branch returns.
The lifecycle sequence
Consider a parent that shows a child only while showDetails is true.
<template>
<button onclick={handleToggle}>Toggle details</button>
<template lwc:if={showDetails}>
<c-detail-card></c-detail-card>
</template>
</template>import { LightningElement } from 'lwc';
export default class Parent extends LightningElement {
showDetails = false;
handleToggle() {
this.showDetails = !this.showDetails;
}
}The child logs each hook so you can watch the order.
import { LightningElement } from 'lwc';
export default class DetailCard extends LightningElement {
clicks = 0;
constructor() {
super();
console.log('detailCard constructor');
}
connectedCallback() {
console.log('detailCard connected');
}
renderedCallback() {
console.log('detailCard rendered');
}
disconnectedCallback() {
console.log('detailCard disconnected');
}
}Each time the condition turns true, the console shows constructor, then connected, then rendered. Each time it turns false, it shows disconnected. The next time the branch appears, a new instance is created, so the constructor runs again.
State does not survive a toggle
Because the instance is replaced, its fields reset to their initial values. If DetailCard tracked a clicks counter, the count returns to 0 after the branch is hidden and shown again.
State you want to keep must live outside the destroyed component. Two common places are:
- The parent, passed down through a public property.
- A shared module or a data service that both sides read.
The same rule applies to switching between branches of one chain. Moving from the lwc:if branch to the lwc:else branch destroys everything in the first branch and creates everything in the second.
Use the hooks for setup and cleanup
The toggle behavior gives you predictable places for work:
| Hook | Runs when | Typical use |
|---|---|---|
| `connectedCallback` | The branch becomes visible and the child is inserted | Fetch data, add listeners |
| `renderedCallback` | After each render of the child | Work that needs the rendered DOM |
| `disconnectedCallback` | The branch goes away | Remove listeners, cancel timers |
Skipping cleanup is a common error. A child that starts a timer in connectedCallback and never clears it in disconnectedCallback keeps running after the child is gone, and every toggle starts another one.
import { LightningElement } from 'lwc';
export default class Ticker extends LightningElement {
intervalId;
seconds = 0;
connectedCallback() {
this.intervalId = setInterval(() => {
this.seconds += 1;
}, 1000);
}
disconnectedCallback() {
clearInterval(this.intervalId);
}
}Quick check
Exercise
A parent shows <c-counter> inside an lwc:if branch. The user clicks a button in the counter until it reads 5, then toggles the branch off and on. What does the counter show, and which hooks ran during the off-and-on toggle?
Show answer
It shows 0. Toggling off runs disconnectedCallback on the old instance. Toggling on creates a new instance, so constructor, connectedCallback, and renderedCallback run and the fields start from their initial values. To keep the count, store it in the parent or a shared module.