Slots let you put custom content inside a component. It helps you reuse components but show different things inside them.
Slots for content projection in Astro
Introduction
Syntax
Astro
<Component> <slot name="slotName"></slot> </Component> <!-- Usage --> <Component> <div slot="slotName">Your content here</div> </Component>
The <slot> tag marks where content will go inside the component.
Named slots use the name attribute to place specific content.
Examples
Astro
--- Card.astro --- <div class="card"> <h2><slot name="title"></slot></h2> <p><slot name="body"></slot></p> </div>
Astro
<Card> <div slot="title">Hello World</div> <div slot="body">This is a simple card.</div> </Card>
Astro
--- Modal.astro --- <div class="modal"> <slot></slot> </div>
Astro
<Modal>
<p>This is inside the modal!</p>
</Modal>Sample Program
This example shows a button component with a default slot. You can put any content inside the button when you use it.
Astro
--- Button.astro --- <button class="btn"> <slot></slot> </button> --- Usage.astro --- --- import Button from './Button.astro'; --- <Button>Click me!</Button> <Button><strong>Bold Text</strong></Button>
Important Notes
If you don't provide content for a slot, it will be empty unless you add fallback content inside the slot tag.
Slots help keep components flexible and reusable by letting you change inner content easily.
Summary
Slots let you insert custom content inside components.
Named slots let you place different pieces of content in specific places.
Use slots to make components flexible and reusable.
Practice
1. What is the main purpose of using
slots in Astro components?easy
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]
Hint: Slots = placeholders for outside content inside components [OK]
Common Mistakes:
- Thinking slots manage styles or state
- Confusing slots with data fetching
- Believing slots are only for static content
2. Which of the following is the correct syntax to define a named slot called
header in an Astro component?easy
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]
Hint: Named slots use syntax [OK]
Common Mistakes:
- Using custom tags like
- Confusing named slots with other frameworks
- Omitting the name attribute
3. Given this Astro component usage:
And inside
What will be rendered?
<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?
medium
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]
Hint: Named slot content fills named slot; others fill default slot [OK]
Common Mistakes:
- Assuming order swaps between slots
- Ignoring slot attribute on elements
- Thinking default slot only renders named slot content
4. What is wrong with this Astro component usage?
And inside
<MyComponent> <div slot="footer">Footer content</div> </MyComponent>
And inside
MyComponent.astro you have:<slot name="header" /> <slot />
medium
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]
Hint: Slot names must match exactly between usage and component [OK]
Common Mistakes:
- Thinking any slot name works without definition
- Believing slot attribute restricts element types
- Assuming default slot needs a name
5. You want to create a reusable
Card component with slots for header, body, and footer. Which Astro component code correctly sets up these named slots?hard
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]
Hint: Use self-closing for named slots [OK]
Common Mistakes:
- Using custom tags instead of <slot>
- Nesting slots inside each other
- Using non-self-closing slot tags
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
