To check whether a component received children in Astro, call Astro.slots.has('default') for the default slot or Astro.slots.has('name') for a named slot. It returns true when the parent passed content for that slot, so you can skip an empty wrapper like a <footer> or show a fallback. Astro.slots.render('name') goes one step further and returns the slot’s rendered HTML as a string.
Key Takeaways
Astro.slots.has('name')tells you whether a slot was given content.- Use it to avoid empty wrappers, borders and spacing when a slot is unused.
<slot>fallback content covers the simple case without any check.Astro.slots.render()returns HTML and can pass arguments to a function child.
Skip empty wrappers
A card with an optional footer:
---
// src/components/Card.astro
interface Props { title: string }
const { title } = Astro.props;
const hasFooter = Astro.slots.has('footer');
---
<article class="card">
<h3>{title}</h3>
<div class="card-body"><slot /></div>
{hasFooter && (
<footer class="card-footer">
<slot name="footer" />
</footer>
)}
</article>
---
// src/pages/index.astro
import Card from '../components/Card.astro';
---
<Card title="Starter">
<p>Everything you need to launch.</p>
<a slot="footer" href="/pricing/">See pricing</a>
</Card>
<Card title="Notes">
<p>No footer here, and no empty footer element either.</p>
</Card>
Without the check, the second card would render an empty <footer> with its border and padding.
Fallback content, no check needed
If you only want default content when nothing is passed, put it inside <slot>:
<slot name="icon">
<svg aria-hidden="true" width="16" height="16"><circle cx="8" cy="8" r="6" /></svg>
</slot>
The fallback renders only when the parent provides nothing for that slot. Use Astro.slots.has() when the decision affects markup around the slot, such as a wrapper or a class.
Changing classes based on slots
---
// src/components/MediaObject.astro
const hasMedia = Astro.slots.has('media');
---
<div class:list={['media', { 'media-has-image': hasMedia }]}>
{hasMedia && <div class="media-figure"><slot name="media" /></div>}
<div class="media-body"><slot /></div>
</div>
The layout switches to a two-column grid only when an image is passed.
Rendering a slot to a string
Astro.slots.render() returns a promise with the slot’s HTML. That’s useful when you need to inspect or reuse it, for example to count words or pass it to set:html:
---
// src/components/Excerpt.astro
const html = Astro.slots.has('default') ? await Astro.slots.render('default') : '';
const words = html.replace(/<[^>]+>/g, ' ').trim().split(/\s+/).filter(Boolean).length;
---
<div class="excerpt" data-words={words} set:html={html} />
Note that the default slot’s name is 'default'.
Passing arguments to a slot function
If the parent passes a function as a child, Astro.slots.render can call it with arguments, which gives you render props:
---
// src/components/List.astro
interface Props { items: string[] }
const { items } = Astro.props;
const rendered = await Promise.all(items.map((item, i) => Astro.slots.render('default', [item, i])));
---
<ul>{rendered.map((html) => <li set:html={html} />)}</ul>
---
import List from '../components/List.astro';
---
<List items={['Astro', 'Vite', 'MDX']}>
{(item: string, i: number) => <span>{i + 1}. {item}</span>}
</List>
This is an advanced pattern; most components only need has().
Things to know
Astro.slots.has()checks whether content was passed, not whether it renders to something visible. A slot containing only{false}still counts.- Slots passed to framework islands arrive as props or children, not through
Astro.slots; see passing props to islands. - Layouts use the same API, which is handy for optional sidebars in nested layouts.
The Astro components guide covers slots from the beginning, and the pages docs show how layouts and components fit together in a Komal theme. Every one of our Astro themes uses named slots for headers, sidebars and footers.
FAQ
How do I check if a slot is empty in Astro?
Use Astro.slots.has('name'), or Astro.slots.has('default') for children passed without a slot name. It returns false when the parent passed nothing for that slot.
What is the name of the default slot in Astro?
'default'. Children without a slot attribute go to the default slot, so check them with Astro.slots.has('default').
Can I use Astro.slots in a React component?
No. Astro.slots exists only in .astro components. React islands receive slot content as children and named props.