0
0
Svelteframework~10 mins

svelte:component for dynamic components - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - svelte:component for dynamic components
Start
Determine component to render
Use <svelte:component this={component} />
Render chosen component in DOM
User interaction or state change
Change component variable
Re-render with new component
End
This flow shows how Svelte dynamically chooses which component to render using <svelte:component> and updates when the chosen component changes.
Execution Sample
Svelte
<script>
  import A from './A.svelte';
  import B from './B.svelte';
  let component = A;
</script>

<svelte:component this={component} />
This code renders component A initially, and can switch to component B by changing the 'component' variable.
Execution Table
StepActionComponent VariableRendered ComponentDOM Change
1Initialize component variableANone yetNo DOM rendered
2Render <svelte:component this={component} />AADOM shows component A content
3User triggers change: component = BBANo immediate DOM change
4Svelte detects component change and re-rendersBBDOM updates to show component B content
5No further changesBBDOM remains showing component B
💡 No more changes; component variable stable at B, rendering stops updating.
Variable Tracker
VariableStartAfter Step 3Final
componentABB
Key Moments - 2 Insights
Why does the DOM update only after step 4, not immediately at step 3 when component changes?
At step 3, the variable changes but Svelte updates the DOM only after detecting the change and running the reactive update cycle at step 4, as shown in the execution_table.
Can <svelte:component> render any value assigned to 'component'?
No, 'component' must be a valid Svelte component or null. If invalid, rendering fails or shows nothing. The execution_table shows valid components A and B used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the rendered component at step 2?
ANo component rendered
BComponent A
CComponent B
DBoth components A and B
💡 Hint
Check the 'Rendered Component' column at step 2 in the execution_table.
At which step does the DOM update to show component B?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'DOM Change' column to see when the DOM updates to component B.
If the 'component' variable was set to null at step 3, what would happen to the DOM?
ADOM would show component A
BDOM would show component B
CDOM would show nothing (empty)
DDOM would show an error message
💡 Hint
Recall that renders nothing if 'this' is null, as implied by the variable_tracker and concept.
Concept Snapshot
<svelte:component this={component} />
- Dynamically renders the component assigned to 'component'
- Change 'component' variable to switch rendered component
- Useful for showing different UI parts without if/else
- 'component' must be a valid Svelte component or null
- DOM updates reactively when 'component' changes
Full Transcript
This visual execution trace shows how Svelte's <svelte:component> tag dynamically renders components based on a variable. Initially, the variable 'component' is set to component A, so the DOM renders A. When the variable changes to component B, Svelte detects this change and updates the DOM to render B. The variable tracker shows the 'component' variable changing from A to B. Key moments clarify that DOM updates happen after Svelte's reactive cycle, not immediately on variable assignment. The visual quiz tests understanding of which component renders at each step and what happens if the variable is null. This helps beginners see how dynamic components work in Svelte step-by-step.