Sub topic 7
Detecting content changes with slotchange
This chapter shows how a child component can find out when the parent adds or removes slotted content. It assumes you can build a basic Lightning web component and use @api properties. It also assumes you know how default slots, named slots, and fallback content work, and that you are comfortable with DOM event handlers.
The problem: the child can't see the parent's decisions
A child component defines the slots, but the parent decides what goes into them. The child can't know ahead of time whether a slot will be filled. A card component might want to render a footer container with padding and a border only when the parent supplies footer content. Fallback content doesn't help here, because it fills the slot itself and doesn't let you hide the container around it.
The browser gives the <slot> element an event for this case. It is called slotchange.
How slotchange works
A slotchange event fires on a <slot> element when the list of nodes assigned to that slot changes. You listen for it with the onslotchange attribute, like any other template event handler.
Three timing facts matter:
- It fires after the component renders, when the slot first receives assigned nodes.
- It fires again when the parent adds or removes nodes in that slot, for example through an
if:trueblock or afor:eachloop. - It does not fire on initial render if the slot has no assigned nodes. Your starting state must therefore assume "empty".
The event target is the <slot> element. Calling assignedNodes() on it returns the nodes the parent put there.
A worked example
The card's footer slot listens for the event. The wrapper <footer> gets its class from a getter.
<!-- card.html (footer section) -->
<footer class={footerClass}>
<slot name="footer" onslotchange={handleFooterChange}></slot>
</footer>The handler counts the assigned nodes and stores the answer in a field.
// card.js
import { LightningElement } from 'lwc';
export default class Card extends LightningElement {
hasFooter = false;
handleFooterChange(event) {
const slot = event.target;
this.hasFooter = slot.assignedNodes().length > 0;
}
get footerClass() {
return this.hasFooter ? 'footer' : 'footer slds-hide';
}
}Here is how it behaves:
- On first render,
hasFooterisfalse, so the footer has theslds-hideclass. - If the parent supplies
<lightning-button slot="footer">, the slot firesslotchange. - The handler sets
hasFootertotrue. Class fields are reactive in LWC, so the component rerenders and the footer becomes visible. - If the parent later removes the button, the event fires again, the count drops to
0, and the footer hides.
Here is a parent that toggles the footer, so you can watch this happen:
<!-- parent.html -->
<template>
<c-card>
<span slot="header">Order summary</span>
<p>Two items in your cart.</p>
<template if:true={showCheckout}>
<lightning-button slot="footer" label="Checkout"></lightning-button>
</template>
</c-card>
<lightning-button label="Toggle footer" onclick={toggle}></lightning-button>
</template>// parent.js
import { LightningElement } from 'lwc';
export default class Parent extends LightningElement {
showCheckout = true;
toggle() {
this.showCheckout = !this.showCheckout;
}
}The slot="footer" attribute sits on the lightning-button inside the if:true template. The parent's conditional decides whether the node exists, and the child only reacts to the result.
What slotchange does not tell you
The event reports changes to the list of assigned nodes. It does not report changes inside those nodes. If the parent keeps the same <p> and only changes its text, no event fires.
The event also does not describe fallback content. For an unfilled slot, assignedNodes() returns an empty array even though fallback text is visible. If you want fallback nodes included, call slot.assignedNodes({ flatten: true }). For most "did the parent supply anything?" checks, the plain call is what you want.
assignedNodes() includes text nodes, such as whitespace between elements. If you only care about elements, use assignedElements(), which skips text nodes.
handleFooterChange(event) {
this.hasFooter = event.target.assignedElements().length > 0;
}Avoiding extra rerenders
The event can fire more than once during startup, for example when several slots are filled in the same render. Assigning the same value to a field again does not change what is displayed, so the handler above is safe to repeat. Keep handlers idempotent. Compute state from the current slot contents instead of toggling a flag, so a duplicate event can't leave the component in the wrong state.
Quick reference
| Question | Answer |
|---|---|
| Where do you attach the handler? | On the `<slot>` element with `onslotchange` |
| What is `event.target`? | The `<slot>` element |
| Fires when the slot is empty at startup? | No |
| Fires when slotted text changes? | No, only when assigned nodes are added or removed |
| Which method lists the nodes? | `assignedNodes()`, or `assignedElements()` for elements only |
Exercise
A parent renders <c-card> with no footer content. The card listens with onslotchange on the footer slot and sets hasFooter inside the handler. The card's footer shows fallback text. Does the handler run on first render, and what does assignedNodes() return if it later runs?
Show answer
The handler does not run on first render, because no nodes are assigned to the slot and slotchange doesn't fire for an empty slot. So hasFooter must default to false. If the handler later runs, assignedNodes() returns only the nodes the parent supplied. Fallback content is not included unless you pass { flatten: true }.
Exercise
The parent keeps one <p slot="footer"> element and updates its text from "Pending" to "Paid". Will slotchange fire? Why or why not?
Show answer
No. The same node stays assigned to the slot. slotchange fires only when nodes are added to or removed from the slot's assigned list, not when the content of an assigned node changes.