📖 Scenario: You are building a simple Svelte app that shows a list of fruits with their colors. You want to split the UI into smaller parts for better organization.
🎯 Goal: Create two Svelte components: FruitItem.svelte to show one fruit and its color, and FruitList.svelte to show the list of fruits using FruitItem. Compose them so the app displays all fruits with their colors.
📋 What You'll Learn
Create a FruitItem.svelte component that accepts name and color as props and displays them.
Create a FruitList.svelte component that has a list of fruits as an array of objects with name and color.
Use FruitItem inside FruitList to render each fruit.
Ensure the main app file App.svelte renders FruitList.
💡 Why This Matters
🌍 Real World
Component composition is how you build complex user interfaces by combining small, reusable parts. This makes your code easier to manage and update.
💼 Career
Understanding component composition is essential for working with modern frontend frameworks like Svelte, React, or Vue. It is a core skill for frontend developers.
Progress0 / 4 steps
1
Create the FruitItem.svelte component
Create a Svelte component file named FruitItem.svelte. Inside it, declare two props: name and color. Then add markup that shows the fruit name inside a <strong> tag and the color inside a <em> tag.
Svelte
Hint
Use export let to declare props in Svelte.
2
Create the FruitList.svelte component with fruit data
Create a Svelte component file named FruitList.svelte. Inside it, create a variable called fruits that is an array of objects. Each object should have name and color properties with these exact entries: { name: 'Apple', color: 'red' }, { name: 'Banana', color: 'yellow' }, and { name: 'Grape', color: 'purple' }.
Svelte
Hint
Use const fruits = [...] to create the array.
3
Use FruitItem inside FruitList.svelte to render each fruit
In FruitList.svelte, import the FruitItem component. Then use a {#each} block to loop over the fruits array. For each fruit, render <FruitItem /> passing name and color as props from the fruit object.
Svelte
Hint
Use import FruitItem from './FruitItem.svelte' and a {#each} block.
4
Render FruitList inside App.svelte
In the main app file App.svelte, import the FruitList component. Then add <FruitList /> inside the markup to show the fruit list on the page.
Svelte
Hint
Use import FruitList from './FruitList.svelte' and add <FruitList /> in markup.
Practice
(1/5)
1. What is the main purpose of component composition in Svelte?
easy
A. To write all UI code in one big file
B. To build UI by combining smaller components
C. To avoid using any components
D. To make components run faster
Solution
Step 1: Understand component composition
Component composition means creating a UI by putting smaller components together.
Step 2: Compare options
Only To build UI by combining smaller components correctly describes this purpose. Other options are incorrect or unrelated.
Final Answer:
To build UI by combining smaller components -> Option B
Quick Check:
Component composition = combining components [OK]
Hint: Think: How do you build complex UI simply? [OK]
Common Mistakes:
Thinking composition means one big file
Believing components slow down the app
Ignoring the reuse benefit
2. Which of the following is the correct way to include a child component Child.svelte inside Parent.svelte?
easy
A. <script>import Child from './Child.svelte';</script><Child />
B. <Child /> without import
C. <script>import Child from './Child.svelte';</script><child />
D. <script>import './Child.svelte';</script><Child />
Solution
Step 1: Check import syntax
In Svelte, you must import the component with a name, e.g., import Child from './Child.svelte'.
Step 2: Use the imported component tag
Use <Child /> with capital C matching import name. <script>import Child from './Child.svelte';</script><Child /> matches this exactly.
Final Answer:
<script>import Child from './Child.svelte';</script><Child /> -> Option A
Quick Check:
Import + matching tag = correct syntax [OK]
Hint: Always import before using component tags [OK]
Common Mistakes:
Using component tags without import
Using lowercase tag names for imported components
Importing without naming the component
3. Given these two components, what will be rendered inside App.svelte?
// Child.svelte
<h2>Hello from Child</h2>
// App.svelte
<script>
import Child from './Child.svelte';
</script>
<main>
<h1>Welcome</h1>
<Child />
</main>
medium
A.
Hello from Child
B.
Welcome
C.
Welcome
Hello from Child
D. Nothing renders due to error
Solution
Step 1: Analyze App.svelte structure
App renders a main with <h1>Welcome</h1> and includes <Child /> component.
Step 2: Check Child.svelte output
Child renders <h2>Hello from Child</h2>.
Step 3: Combine outputs
App will show <h1>Welcome</h1> followed by Child's <h2>Hello from Child</h2>.
Final Answer:
<h1>Welcome</h1><h2>Hello from Child</h2> -> Option C
Quick Check:
Parent + child render combined [OK]
Hint: Child component content appears where tag is placed [OK]
Common Mistakes:
Ignoring child component output
Thinking child replaces parent content
Assuming syntax error without cause
4. What is wrong with this Svelte component composition code?
A. Component tag <child /> should be capitalized as <Child />
B. Missing import statement for Child
C. Parent component cannot use Child component
D. The <main> tag is invalid here
Solution
Step 1: Check import and usage
Child is imported as Child with capital C.
Step 2: Check component tag case
Svelte requires component tags to match import name case exactly. <child /> is lowercase and invalid.
Final Answer:
Component tag <child /> should be capitalized as <Child /> -> Option A
Quick Check:
Component tags are case-sensitive [OK]
Hint: Match component tag case to import name exactly [OK]
Common Mistakes:
Using lowercase component tags
Forgetting case sensitivity
Assuming missing import
5. You want to create a Dashboard.svelte that shows a Header, a Sidebar, and a Content component. Which approach correctly composes these components to keep the layout organized and reusable?
hard
A. <script>import Header from './Header.svelte'; import Sidebar from './Sidebar.svelte'; import Content from './Content.svelte';</script>
<Header />
<Sidebar />
B. <script>import Header from './Header.svelte';</script>
<Header />
<Sidebar />
<Content />
C. <script>import * from './components';</script>
<Header /><Sidebar /><Content />
D. <script>import Header from './Header.svelte'; import Sidebar from './Sidebar.svelte'; import Content from './Content.svelte';</script>
<div class='dashboard'>
<Header />
<Sidebar />
<Content />
</div>
Solution
Step 1: Check imports for all components
<script>import Header from './Header.svelte'; import Sidebar from './Sidebar.svelte'; import Content from './Content.svelte';</script>
<div class='dashboard'>
<Header />
<Sidebar />
<Content />
</div> imports Header, Sidebar, and Content correctly.
Step 2: Check usage inside a container
<script>import Header from './Header.svelte'; import Sidebar from './Sidebar.svelte'; import Content from './Content.svelte';</script>
<div class='dashboard'>
<Header />
<Sidebar />
<Content />
</div> uses a container div to organize components, making layout clear and reusable.
Step 3: Compare other options
<script>import Header from './Header.svelte';</script>
<Header />
<Sidebar />
<Content /> misses imports for Sidebar and Content. <script>import * from './components';</script>
<Header /><Sidebar /><Content /> uses invalid import syntax. <script>import Header from './Header.svelte'; import Sidebar from './Sidebar.svelte'; import Content from './Content.svelte';</script>
<Header />
<Sidebar /> misses Content usage.
Final Answer:
<script>import Header from './Header.svelte'; import Sidebar from './Sidebar.svelte'; import Content from './Content.svelte';</script>
<div class='dashboard'>
<Header />
<Sidebar />
<Content />
</div> -> Option D
Quick Check:
Import all + use all components inside container [OK]
Hint: Import and use all components inside one container [OK]
Common Mistakes:
Forgetting to import all components
Using invalid import syntax
Not including all components in layout
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.