Discover how a single tag can make your app switch views like magic!
Why svelte:component for dynamic components? - Purpose & Use Cases
Imagine you want to show different parts of your app based on user choices, like switching between a login form, a profile view, or a settings page.
Doing this by hand means writing lots of if-else blocks and manually adding or removing components in your code.
Manually swapping components is messy and hard to maintain.
You have to write repetitive code and it's easy to make mistakes, like forgetting to update something or causing flickers on the screen.
The svelte:component tag lets you pick which component to show dynamically with a simple variable.
This means you write cleaner code and Svelte handles the switching smoothly for you.
if (page === 'login') { show LoginComponent } else if (page === 'profile') { show ProfileComponent }
<svelte:component this={currentComponent} />You can build flexible apps that change views instantly based on user actions, without messy code.
Think of a dashboard app where clicking menu items instantly swaps the main content area to different tools or reports.
Manually switching components is repetitive and error-prone.
svelte:component lets you swap components easily using a variable.
This makes your code cleaner and your app more responsive.
Practice
svelte:component tag do in Svelte?Solution
Step 1: Understand the purpose of
This tag is used to render different components dynamically depending on a variable's value.svelte:componentStep 2: Compare with other options
Creating files, styling, or importing are unrelated tosvelte:component.Final Answer:
It renders a component dynamically based on a variable. -> Option CQuick Check:
svelte:component= dynamic rendering [OK]
svelte:component means dynamic component rendering [OK]- Thinking it creates new component files
- Confusing it with CSS styling
- Assuming it imports components
CurrentComp?Solution
Step 1: Recall correct tag and attribute
The correct tag issvelte:componentand the attribute to specify the component isthis.Step 2: Check each option
<svelte:component this={CurrentComp} />uses correct tag and attribute syntax. Others use wrong tags or attributes.Final Answer:
<svelte:component this={CurrentComp} /> -> Option BQuick Check:
Correct tag andthisattribute =<svelte:component this={CurrentComp} />[OK]
svelte:component this={variable} for dynamic components [OK]- Using
componentinstead ofsvelte:component - Using
srcinstead ofthis - Capitalizing tags incorrectly
<script>
import A from './A.svelte';
import B from './B.svelte';
let comp = A;
</script>
<svelte:component this={comp} />
<button on:click={() => comp = B}>Switch</button>What happens when the button is clicked?
Solution
Step 1: Understand the initial rendering
The variablecompstarts as component A, so A renders initially.Step 2: Analyze the button click
Clicking the button setscompto B, which triggers Svelte to render component B dynamically.Final Answer:
The displayed component switches from A to B. -> Option AQuick Check:
Dynamicthischanges component = The displayed component switches from A to B. [OK]
this variable updates the rendered component [OK]- Thinking dynamic components can't switch
- Assuming comp is not reactive
- Expecting both components to render simultaneously
<script>
import X from './X.svelte';
let comp = 'X';
</script>
<svelte:component this={comp} />Solution
Step 1: Check the type of
The variablecompcompis assigned the string 'X', but it should hold the imported component itself.Step 2: Understand
thisattribute requirementsthisexpects a component reference, not a string name.Final Answer:
The variable comp should hold the component, not a string. -> Option DQuick Check:
thisneeds component, not string [OK]
this [OK]- Assigning component name as string
- Thinking
svelte:componentcan't be self-closing - Believing variables can't be used in
this
Cat and Dog both accept a name prop, how do you pass name="Buddy" to the dynamic component stored in petComp?Solution
Step 1: Recall how to pass props to components
Props are passed as attributes directly on the component tag, includingsvelte:component.Step 2: Check each option's syntax
<svelte:component this={petComp} name="Buddy" />correctly passesname="Buddy".<svelte:component this={petComp} {name}="Buddy" />uses invalid syntax.<svelte:component this={petComp} props={{name: 'Buddy'}} />tries to pass props as an object, which is not supported.<svelte:component this={petComp} bind:name="Buddy" />usesbind:incorrectly.Final Answer:
<svelte:component this={petComp} name="Buddy" /> -> Option AQuick Check:
Pass props as attributes onsvelte:component[OK]
svelte:component [OK]- Trying to pass props as an object
- Using bind: incorrectly
- Using curly braces around prop name with value
Ready to prove it?
Pass the test and this pattern moves unknown
