Discover how slots save you from endless copying and make your components truly reusable!
Why Slots for content projection in Astro? - Purpose & Use Cases
Imagine building a webpage where you want to reuse a card component but show different content inside it each time. You try copying the card HTML and changing the content manually for every use.
Manually copying and changing content is slow and error-prone. If you want to update the card style, you must change every copy. It's easy to forget one and end up with inconsistent pages.
Slots let you create a reusable component with placeholders. You write the card once and fill in different content each time you use it, keeping your code clean and easy to update.
<div class="card"> <p>Content 1</p> </div> <div class="card"> <p>Content 2</p> </div>
<Card> <p>Content 1</p> </Card> <Card> <p>Content 2</p> </Card>
Slots enable flexible, reusable components that adapt to different content without rewriting code.
Think of a restaurant menu where the frame stays the same but the dishes inside change daily. Slots let you swap dishes easily without redesigning the menu every day.
Manual content changes cause repetition and errors.
Slots create placeholders for flexible content insertion.
This makes components reusable and easier to maintain.
Practice
slots in Astro components?Solution
Step 1: Understand what slots do
Slots allow you to place custom content inside a component from outside it, making the component flexible.Step 2: Compare with other options
Styles, state, and data fetching are unrelated to slots' main purpose.Final Answer:
To insert custom content inside a component from outside -> Option CQuick Check:
Slots = insert custom content [OK]
- Thinking slots manage styles or state
- Confusing slots with data fetching
- Believing slots are only for static content
header in an Astro component?Solution
Step 1: Recall Astro slot syntax
Named slots use the syntax to define where content goes.Step 2: Check other options
Other syntaxes like , , or are invalid in Astro.Final Answer:
<slot name="header" /> -> Option DQuick Check:
Named slot syntax = <slot name="..." /> [OK]
- Using custom tags like
- Confusing named slots with other frameworks
- Omitting the name attribute
<MyComponent> <h1 slot="title">Welcome</h1> <p>Enjoy your stay</p> </MyComponent>
And inside
MyComponent.astro you have:<slot name="title" /> <slot />
What will be rendered?
Solution
Step 1: Match named slot content
The <h1> has slot="title", so it fills <slot name="title" /> in the component.Step 2: Match default slot content
The <p> has no slot attribute, so it fills the default <slot />.Final Answer:
<h1>Welcome</h1><p>Enjoy your stay</p> -> Option AQuick Check:
Named slot + default slot = both contents rendered [OK]
- Assuming order swaps between slots
- Ignoring slot attribute on elements
- Thinking default slot only renders named slot content
<MyComponent> <div slot="footer">Footer content</div> </MyComponent>
And inside
MyComponent.astro you have:<slot name="header" /> <slot />
Solution
Step 1: Check slot names in component
The component defines slots named "header" and default (unnamed), but no "footer" slot.Step 2: Check usage slot names
The usage tries to fill a "footer" slot which does not exist, so content won't render.Final Answer:
The slot named "footer" is not defined inside the component -> Option AQuick Check:
Undefined slot name = no content rendered [OK]
- Thinking any slot name works without definition
- Believing slot attribute restricts element types
- Assuming default slot needs a name
Card component with slots for header, body, and footer. Which Astro component code correctly sets up these named slots?Solution
Step 1: Recall correct slot syntax
Named slots in Astro use self-closing tags like <slot name="name" /> to define placeholders.Step 2: Evaluate options
<slot name="header" /> <slot name="body" /> <slot name="footer" /> uses correct self-closing syntax for all three slots. <slot-header /> <slot-body /> <slot-footer /> uses invalid custom tags. <slot name="header"> <slot name="body"> <slot name="footer"> nests slots incorrectly. <slot name="header"></slot> <slot name="body"></slot> <slot name="footer"></slot> uses non-self-closing tags which Astro does not support for slots.Final Answer:
<slot name="header" /> <slot name="body" /> <slot name="footer" /> -> Option BQuick Check:
Named slots = self-closing <slot name="..." /> [OK]
- Using custom tags instead of <slot>
- Nesting slots inside each other
- Using non-self-closing slot tags
Ready to prove it?
Pass the test and this pattern moves unknown
