Challenge - 5 Problems
Svelte in Astro Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the rendered output of this Astro page using a Svelte component?
Given the Astro page imports a Svelte component that displays a greeting with a passed name prop, what will the user see when visiting this page?
Astro
/* Astro page: */ --- import Greeting from '../components/Greeting.svelte'; const userName = 'Alex'; --- <html lang="en"> <body> <Greeting name={userName} /> </body> </html> /* Greeting.svelte component: */ <script> export let name; </script> <h1>Hello, {name}!</h1>
Attempts:
2 left
💡 Hint
Remember how props are passed from Astro to Svelte components.
✗ Incorrect
The Astro page passes the variable userName as the name prop to the Svelte component. The component uses this prop to render 'Hello, Alex!'.
📝 Syntax
intermediate1:30remaining
Which option correctly imports and uses a Svelte component in an Astro file?
Select the correct syntax to import and use a Svelte component named Button.svelte inside an Astro page.
Attempts:
2 left
💡 Hint
Astro requires default import for Svelte components with .svelte extension.
✗ Incorrect
Svelte components are default exports, so you import them with 'import Button from ...'. The component is then used as in the Astro template.
🔧 Debug
advanced2:30remaining
Why does this Astro page fail to hydrate the Svelte component?
This Astro page imports a Svelte component but the page shows static HTML instead of the hydrated component content. What is the cause?
Astro
--- import Counter from '../components/Counter.svelte'; --- <html lang="en"> <body> <Counter count={5} /> </body> </html> /* Counter.svelte */ <script> export let count; </script> <p>Count is {count}</p>
Attempts:
2 left
💡 Hint
Think about how Astro handles client-side interactivity with Svelte components.
✗ Incorrect
Astro renders Svelte components as static HTML by default. To enable interactivity or dynamic props, you must add a client directive like client:load or client:visible.
❓ state_output
advanced2:00remaining
What is the displayed count after clicking the button twice in this Svelte component used in Astro?
The Counter.svelte component increments count on button click. What is the displayed count after two clicks?
Astro
<script> export let initial = 0; let count = initial; function increment() { count += 1; } </script> <button on:click={increment}>Clicked {count} times</button>
Attempts:
2 left
💡 Hint
Each click calls increment which adds 1 to count starting from initial 0.
✗ Incorrect
The count starts at 0 and increments by 1 on each click. After two clicks, count is 2, so the button text shows 'Clicked 2 times'.
🧠 Conceptual
expert1:30remaining
Which client directive should you use to hydrate a Svelte component only when it becomes visible in the viewport?
You want to optimize performance by hydrating a Svelte component in Astro only when the user scrolls it into view. Which client directive achieves this?
Attempts:
2 left
💡 Hint
Think about lazy hydration triggered by visibility.
✗ Incorrect
The client:visible directive hydrates the component only when it enters the viewport, improving performance by delaying hydration until needed.