Consider the following Svelte component usage:
<Card>Hello, world!</Card>
And the Card.svelte component code:
<div class="card"> <slot>Default content</slot> </div>
What will be rendered inside the <div>?
<div class="card"> <slot>Default content</slot> </div>
Think about what happens when you put content between component tags and the component has a default slot.
The default slot displays any content passed between the component tags. Here, 'Hello, world!' replaces the default slot content.
Choose the correct syntax to create a default slot with fallback content 'Loading...'.
Default slots do not need a name attribute.
The default slot is created simply with <slot> and fallback content inside it. Named slots require a name attribute, but default slots do not.
Given this parent component:
<script>
import Card from './Card.svelte';
let message = 'Hello';
setTimeout(() => message = 'Goodbye', 1000);
</script>
<Card>{message}</Card>And Card.svelte:
<div class="card"> <slot>Default</slot> </div>
What will the user see after 2 seconds?
Slots update reactively when the parent content changes.
The slot content is reactive. When message changes to 'Goodbye', the slot updates to show the new text.
Look at this component usage:
<Wrapper></Wrapper>
And the component code:
<div> <slot>Fallback content</slot> </div>
Why does 'Fallback content' never appear?
Think about what counts as 'no content' for a slot fallback.
Even empty whitespace or empty tags count as slot content, so fallback only shows if nothing is passed at all.
Consider these components:
// Parent.svelte <Child>Parent content</Child>
// Child.svelte <slot>Child default</slot>
// Grandchild.svelte <slot>Grandchild default</slot>
If Child.svelte uses <Grandchild></Grandchild> inside its slot, what will be rendered?
Remember how slots pass content down and fallback content works at each level.
Parent content fills Child's slot. If Child uses Grandchild without passing content, Grandchild's slot is empty, so its fallback shows.