The svelte:component tag lets you show different components based on a variable. It helps when you want to switch parts of your page without writing many if-else blocks.
svelte:component for dynamic components
<svelte:component this={ComponentVariable} {...props} />this attribute must be a component reference (not a string).
You can pass props to the dynamic component using spread syntax or named props.
Button component dynamically.<script> import Button from './Button.svelte'; let current = Button; </script> <svelte:component this={current} />
RedBox and BlueBox components and passes a size prop.<script> import RedBox from './RedBox.svelte'; import BlueBox from './BlueBox.svelte'; let box = RedBox; </script> <svelte:component this={box} size="large" />
Hello or Goodbye component with a name prop.<script> import Hello from './Hello.svelte'; import Goodbye from './Goodbye.svelte'; let greet = Math.random() > 0.5 ? Hello : Goodbye; </script> <svelte:component this={greet} name="Friend" />
This example shows a button that switches between Cat and Dog components when clicked. The displayed component changes dynamically.
<script> import Cat from './Cat.svelte'; import Dog from './Dog.svelte'; let animal = Cat; function toggle() { animal = animal === Cat ? Dog : Cat; } </script> <button on:click={toggle} aria-label="Toggle animal">Toggle Animal</button> <svelte:component this={animal} />
Make sure the variable used in this is a valid Svelte component.
You can pass any props to the dynamic component just like a normal component.
Use svelte:component to avoid many if-else blocks and keep code clean.
svelte:component lets you render components dynamically based on a variable.
Use the this attribute to specify which component to show.
You can pass props to the dynamic component as usual.
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
