0
0
Svelteframework~10 mins

Component composition in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Component composition
Parent Component
Import Child Component
Use Child in Parent's markup
Render Parent
Child renders inside Parent
User sees combined UI
Component composition means putting smaller components inside bigger ones to build UI step-by-step.
Execution Sample
Svelte
/* Child.svelte */
<script>
  export let message = "";
</script>
<p>{message}</p>

/* Parent.svelte */
<script>
  import Child from './Child.svelte';
</script>
<Child message="Hello from Parent!" />
Parent component imports Child and uses it, passing a message prop to show inside Child.
Execution Table
StepActionComponentState BeforeState AfterRendered Output
1Parent starts renderingParentNo stateImports Child<Child message="Hello from Parent!" />
2Child receives propChildmessage=""message="Hello from Parent!"<p>Hello from Parent!</p>
3Child renders inside ParentParent + ChildParent markup with <Child>Child rendered inside Parent<p>Hello from Parent!</p> inside Parent
4Parent finishes renderingParentChild renderedFull UI readyUser sees: Hello from Parent!
💡 Rendering completes when Parent and all children finish rendering.
Variable Tracker
VariableStartAfter Step 2Final
message"""Hello from Parent!""Hello from Parent!"
Key Moments - 2 Insights
Why does the Child component show the message from Parent?
Because Parent passes the message as a prop to Child (see execution_table step 2), Child uses it to render its content.
What happens if Parent does not import Child?
Parent cannot use Child in its markup, so Child won't render (see execution_table step 1). Import is required to compose components.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'message' in Child after step 2?
Aundefined
B"Hello from Parent!"
C""
D"Child message"
💡 Hint
Check the 'State After' column for step 2 in execution_table.
At which step does the Child component finish rendering inside Parent?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for when Child is rendered inside Parent in execution_table.
If Parent does not pass the 'message' prop, what will Child display?
AEmpty paragraph
BAn error
C"Hello from Parent!"
DNothing renders
💡 Hint
Refer to variable_tracker and how 'message' changes; default is empty string.
Concept Snapshot
Component composition in Svelte:
- Import child with: import Child from './Child.svelte';
- Use child in parent markup: <Child prop="value" />
- Pass data via props (export let propName)
- Child renders inside Parent's UI
- Builds UI by nesting components
Full Transcript
Component composition in Svelte means building UI by putting smaller components inside bigger ones. The Parent component imports the Child component and uses it in its markup. The Parent passes data to Child using props. Child receives the prop and renders content based on it. The rendering process starts with Parent, then Child renders inside Parent, and finally the full UI is shown to the user. Variables like props change as data flows from Parent to Child. Importing Child is necessary to use it. If no prop is passed, Child shows default or empty content.