Sub topic 12
When a slot is a better fit than a property
This chapter assumes you can build a Lightning web component, use @api properties, and bind values in a template. You will learn when to pass markup to a child through a slot instead of passing a value through a property.
Properties pass values, slots pass markup
A public property gives a child a value, such as a string or an object. The child decides how to display it. A slot gives a child a place where the parent supplies the markup itself. The child provides the frame, and the parent fills it.
Use a property when the child owns the layout and only needs data. Use a slot when the parent should control what appears inside the child.
A child with a slot
The child below draws a card with a heading and a body area. The <slot> element marks where the parent's content goes.
<!-- infoCard.html (child) -->
<template>
<article>
<h3>{heading}</h3>
<div class="body">
<slot></slot>
</div>
</article>
</template>// infoCard.js (child)
import { LightningElement, api } from 'lwc';
export default class InfoCard extends LightningElement {
@api heading;
}The parent puts content between the opening and closing tags of the child.
<!-- app.html (parent) -->
<template>
<c-info-card heading="Release notes">
<p>Version 2 adds <strong>dark mode</strong>.</p>
<a href="/notes">Read more</a>
</c-info-card>
</template>The heading is a short value, so it stays a property. The body is free-form markup, so it goes in the slot. A property would be awkward for the body, because it would have to carry HTML as a string or an object that describes each element.
Named slots
A child can offer more than one place. Give each extra <slot> a name. The parent targets it with the slot attribute. Content without a slot attribute goes to the unnamed slot.
<!-- panel.html (child) -->
<template>
<section>
<header><slot name="title"></slot></header>
<div><slot></slot></div>
<footer><slot name="actions"></slot></footer>
</section>
</template><!-- app.html (parent) -->
<template>
<c-panel>
<span slot="title">Account summary</span>
<p>Your balance is up this month.</p>
<button slot="actions" onclick={handleSave}>Save</button>
</c-panel>
</template>Note that handleSave belongs to the parent. Slotted content is owned by the parent, so its bindings and event handlers are resolved against the parent's class.
Choosing between the options
The diagram shows a quick way to decide.
| Need | Best fit |
|---|---|
| Give the child a string, number, object or array | Public property |
| Let the parent supply layout or components inside the child | Slot |
| Ask the child to do something, such as focus an input | Public method |
| Send data from the child back to the parent | Custom event |
Things to watch for
- A slot does not receive data from the child. The parent cannot read values from the child's scope inside slotted content. If the child must supply the data for each row, use a property and let the child render the rows.
- The child cannot easily inspect slotted content. If the child needs to calculate something from it, a property is a better fit.
- Slotted content renders even when the child does not use it in a way you expect. Keep slots for content that should always have a place to appear.
Exercise
A c-modal child needs a title string, a body that can contain any markup the parent chooses, and a Save button that the parent wires to its own handler. Which parts should be a property and which a slot?
Show answer
The title is a simple value, so make it a public property (@api title). The body and the Save button are parent-supplied markup, so use slots: an unnamed slot for the body and a named slot such as footer for the button. The button's click handler stays in the parent's class.