Sub topic 10
Pitfalls and best practices
This chapter assumes you can build a child component with default and named slots, pass content from a parent with the slot attribute, and handle slotchange. It collects the mistakes that most often break slot-based components and the habits that prevent them.
Remember who owns the content
Slotted markup is written in the parent's template, so the parent owns it. Most slot bugs come from forgetting this.
- Styling: The parent's CSS styles slotted elements. The child's CSS does not reach into them in the general case, so a rule like
.body p { color: red; }in the child has no effect on a paragraph the parent supplied. - Data binding: Expressions such as
{orderTotal}in slotted content resolve against the parent's class. The child's properties are not available there. - Events: Handlers on slotted elements live in the parent's JavaScript. The child can still listen on the
<slot>element, because events from slotted content bubble through it. - Queries:
this.template.querySelector()in the child does not find slotted elements. Usethis.querySelector()or ask the slot for its nodes.
<!-- card.js can find the button with this.querySelector('lightning-button') -->
<c-card>
<lightning-button slot="footer" label="Checkout"></lightning-button>
</c-card>Common pitfalls
| Pitfall | What happens | Fix |
|---|---|---|
| `slot="footer"` on a nested element instead of a direct child of `<c-card>` | The content is not assigned to the slot and does not appear | Put the `slot` attribute on the direct child of the component tag |
| Name mismatch such as `slot="Footer"` against `<slot name="footer">` | Content is dropped or falls into the wrong place | Use exact, lowercase names and keep them in the component's documentation |
| Removing the `<slot>` with `lwc:if` to hide empty areas | `slotchange` can never fire, so the area never comes back | Keep the slot rendered and hide its container with CSS |
| Counting `assignedNodes()` to detect content | Text nodes count as content | Use `assignedElements()` when you only care about elements |
| Assuming `slotchange` runs only on later changes | It can also fire for the initial assignment | Make the handler idempotent and safe on first run |
| Reading slotted content in `connectedCallback` | The slot may not be rendered yet | Read in `renderedCallback` or in the `slotchange` handler |
| Modifying slotted nodes from the child | You change markup the parent owns, and the parent's next render can overwrite it | Treat slotted nodes as read-only |
Keep the slot rendered
This is the most common structural mistake. A conditional that removes the slot also removes the event source.
<!-- Avoid: if the slot is not rendered, slotchange never fires -->
<template lwc:if={hasFooter}>
<footer>
<slot name="footer" onslotchange={handleFooterChange}></slot>
</footer>
</template>Keep the slot in the template and toggle a class on its container instead:
<footer class={footerClass}>
<slot name="footer" onslotchange={handleFooterChange}></slot>
</footer>import { LightningElement } from 'lwc';
export default class Card extends LightningElement {
hasFooter = false;
handleFooterChange(event) {
const slot = event.target;
this.hasFooter = slot.assignedElements().length > 0;
}
get footerClass() {
return this.hasFooter ? 'footer' : 'footer slds-hide';
}
}assignedNodes() and assignedElements() return only content the parent supplied. When the slot shows its fallback text, both return an empty array. That is why this check works for hiding an empty footer.
Decision flow
Best practices
- Use properties for data and slots for markup. A title string belongs in an
@apiproperty. A title that needs an icon and a link belongs in a slot. - Give every named slot fallback content or a clear purpose. Fallback text such as "Untitled" keeps the layout usable when the parent passes nothing.
- Keep the slot count small. A default slot plus two or three named slots is easy to learn. Many slots usually mean the component is doing too much.
- Style at the right level. Let the parent style what it supplies. Let the child style only its own wrapper elements, such as the
header,div, andfooterin the card. - Wrap the slot, not the content. Put layout classes on the child's container elements so the parent's content stays untouched.
- Document slot names. Slot names are part of a component's public interface, as
@apiproperties are. Renaming one breaks every parent that uses it. - Test the empty case. Render the component with no slotted content and confirm that fallback text and hidden containers behave as intended.
Exercise
A developer wraps the footer slot in <template lwc:if={hasFooter}> and sets hasFooter inside the slotchange handler. The footer never appears, even when the parent supplies a button. Why, and how do you fix it?
Show answer
The slot is not rendered while hasFooter is false, so it can't fire slotchange, and hasFooter never becomes true. Always render the <slot>, and hide its container with a CSS class such as slds-hide, toggled from the handler.
Exercise
A parent passes <p slot="body">Hello</p> to <c-card>, but the card only defines <slot name="header">, a default slot, and <slot name="footer">. Where does the paragraph go?
Show answer
Nowhere. No slot is named body, so the paragraph is not rendered. It does not fall into the default slot. Rename the attribute to match an existing slot, or remove the attribute so the paragraph goes to the default slot.