Sub topic 6
Slots or public properties?
This chapter assumes you can build a basic Lightning web component, set an @api property from a parent template, and use default and named slots. Here you will learn how to choose between a slot and a public property when a parent needs to give a child something.
Two ways to hand content to a child
A parent can customize a child in two ways. It can set a public property, or it can place markup inside the child's tags to fill a slot. The two are not interchangeable. Each one moves a different kind of responsibility between parent and child.
With a property, the child receives a value and decides how to render it. With a slot, the parent supplies finished markup and the child only decides where it goes.
Same banner, two designs
Here is a banner that takes its message as a property.
<!-- statusBanner.html -->
<template>
<div class={bannerClass} role="status">{message}</div>
</template>// statusBanner.js
import { LightningElement, api } from 'lwc';
export default class StatusBanner extends LightningElement {
@api message = '';
@api variant = 'info';
get bannerClass() {
return `banner banner-${this.variant}`;
}
}The parent writes <c-status-banner message="Saved" variant="success"></c-status-banner>. The child controls the markup, so every banner looks the same. The limit is that message can only be text. The parent cannot put a link inside it, and it cannot add an icon or a button.
Now the slot version.
<!-- statusBanner.html -->
<template>
<div class="banner" role="status">
<slot>No message</slot>
</div>
</template><!-- parent.html -->
<template>
<c-status-banner>
Saved. <a href="/orders">View your orders</a>
</c-status-banner>
</template>The parent can now pass any markup, including components. The cost is that the child no longer controls what it displays. It cannot validate the content, and it cannot compute a CSS class from the content.
Decision criteria
Ask these questions in order.
- Is the content markup or a component, such as a link, a button, or a formatted list? If so, use a slot.
- Does the child need to read, validate, format, or branch on the value? If so, use a property.
- Is the value short plain text, a number, or a boolean? If so, use a property.
- Is the layout fixed but the content varies widely between uses? If so, use a slot.
Comparing the two
| Concern | Public property | Slot |
|---|---|---|
| What the parent passes | Data (string, number, object, array) | Markup and components |
| Who writes the markup | Child | Parent |
| Styling | Child's CSS applies directly | Parent's CSS applies, because the parent owns the markup |
| Validation or transformation | Child can do both | Child sees only DOM nodes |
| Event handlers | Child handles events on its own elements | Parent attaches handlers to the elements it passes |
| Empty case | Check for `undefined` or an empty string | Fallback content, or `slotchange` |
Combining both
Most well-designed components use both. Use properties for small, well-defined settings that the child interprets, and slots for the open-ended region.
<!-- panel.html -->
<template>
<section class={panelClass}>
<h2>{title}</h2>
<slot></slot>
<footer>
<slot name="actions"></slot>
</footer>
</section>
</template>// panel.js
import { LightningElement, api } from 'lwc';
export default class Panel extends LightningElement {
@api title = '';
@api compact = false;
get panelClass() {
return this.compact ? 'panel panel-compact' : 'panel';
}
}Here title is a property because the child places it inside an h2 and keeps heading styles consistent. compact is a property because the child changes its own class. The body and actions are slots because the parent decides what they contain.
<!-- parent.html -->
<template>
<c-panel title="Shipping" compact>
<p>Delivery takes three to five days.</p>
<lightning-button slot="actions" label="Change address"></lightning-button>
</c-panel>
</template>Common mistakes in this choice
- Using a slot for a single piece of text. A parent writing
<span slot="title">Shipping</span>instead oftitle="Shipping"adds markup for nothing and gives up the child's heading styles. - Using a property for rich content. Passing an HTML string through a property tempts you to render it with
lwc:dom="manual"andinnerHTML. That bypasses the template's escaping and creates injection risk. Use a slot instead. - Creating a slot for every value. A component with five named slots is hard to use and hard to keep consistent. If the child must enforce a design, make that part a property.
- Expecting the child to inspect slot content like data. The child sees DOM nodes through
assignedNodes(). If the child needs the values, a property or a public method is a better contract.
Exercise
A c-price-tag component shows a price. The parent supplies the amount and a currency code, and the child must format the amount with two decimals and place the currency symbol. Should the amount be a slot or a public property, and why?
Show answer
A public property. The child must read and format the value, and it is a plain number. A slot would hand the child raw DOM nodes it would have to parse. A slot would only be justified if the parent needed to put arbitrary markup, such as a struck-through original price or an icon, next to the amount.
Exercise
A c-dialog has a heading, a body that varies on every page, and "Cancel" and "Save" buttons that call parent logic. Which parts would you make properties and which slots?
Show answer
Make the heading a property, because it is short text that the dialog styles consistently. Make the body a default slot, because it can contain any markup. Make the buttons a named footer slot, so the parent writes the buttons with their own click handlers and keeps that logic in the parent.