Complete the code to render a dynamic component using svelte:component.
<script> import Button from './Button.svelte'; let current = [1]; </script> <svelte:component this={current} />
You assign the imported component Button to the variable current. Then svelte:component uses this={current} to render it dynamically.
Complete the code to switch the dynamic component based on a condition.
<script> import A from './A.svelte'; import B from './B.svelte'; let useA = true; let current = useA ? [1] : B; </script> <svelte:component this={current} />
useA instead of the component.The ternary operator chooses A if useA is true, otherwise B. So current must be assigned A or B.
Fix the error in the code to correctly pass props to the dynamic component.
<script> import Card from './Card.svelte'; let current = Card; let title = 'Hello'; </script> <svelte:component this={current} [1] />
title:title.Props are passed like HTML attributes in Svelte. Use title={title} to pass the variable title as a prop.
Fill both blanks to render a dynamic component with a fallback if the component is null.
<script> import X from './X.svelte'; let current = [1]; </script> {#if current} <svelte:component this={current} /> [2] {/if}
current without fallback.{#if} block.Assign X to current. Use an {#if} block to check if current exists. If not, show fallback text like <p>No component selected</p>.
Fill all three blanks to create a dynamic component that updates on button click.
<script> import One from './One.svelte'; import Two from './Two.svelte'; let current = [1]; function toggle() { current = current === One ? [2] : One; } </script> <button on:click=[3]>Toggle</button> <svelte:component this={current} />
toggle() instead of passing the function reference.Start with current = One. The toggle function switches between One and Two. The button's click event uses on:click={toggle} to call the function.