Practice and reinforce the concepts from Lesson 3
export let
to define component propsTotal Activity Time: 45 minutes
💡 Tip Make sure to save your work periodically in the Svelte REPL by bookmarking your playground URL!
Time: 15 minutes
Svelte REPL - Remove all existing code before starting
Let's create a simple player score tracker component.
Steps:
Score.svelte
player
and score
💡 Tip Use
export let
to create props that can receive data from parent components. This is the standard way to define props in Svelte!
Score.svelte:
<script>
export let player;
export let score;
</script>
<div>
<p>Player \{player\}</p>
<p>Score: \{score\}</p>
</div>
<style>
div\{
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.5);
padding: 0.5rem;
margin: 12px 40px;
border-radius: 5px;
text-align: center;
font-weight: bold;
\}
p\{
margin: 0;
\}
</style>
app.svelte:
<script>
import Score from "./Score.svelte";
</script>
<Score player="1" score="100" />
<Score player="2" score="250" />
<Score player="3" score="180" />
If you've done it correctly, it should look like this:
Time: 10 minutes
Svelte REPL - Remove all existing code before starting
Let's create a simple multiply calculator component that accepts two numbers and displays their product.
Steps:
Multiply.svelte
num1
and num2
answer
that calculates num1 * num2
💡 Tip You can calculate values using exported props within the same script section. Svelte will automatically update the calculation when props change!
Multiply.svelte:
<script>
export let num1;
export let num2;
let answer = num1 * num2;
</script>
<p>\{num1\} x \{num2\} = \{answer\}</p>
app.svelte:
<script>
import Multiply from "./Multiply.svelte";
</script>
<Multiply num1="3" num2="8" />
If you've done it correctly, it should look something like this:
Assuming num1
is 3 and num2
is 8.
Time: 20 minutes
Svelte REPL - Remove all existing code before starting
Create a student card component that accepts multiple props using object spread syntax.
Steps:
Card.svelte
school
, name
, studentID
, and year
⚠️ Warning Important: You must use object spread syntax
\{...objectName\}
to pass all props at once, not individual props!
💡 Tip Object spread syntax is great when you have many props to pass. It helps keep your code clean and maintainable.
Card.svelte:
<script>
export let school;
export let name;
export let studentID;
export let year;
</script>
<div class="card">
<h1>\{school\}</h1>
<p class="stu">Student</p>
<h1>:)</h1>
<p>\{name\}</p>
<p>\{studentID\}</p>
<p>INTAKE: \{year\}</p>
</div>
<style>
.card \{
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.5);
padding: 2px 16px;
margin: 12px 4px;
border-radius: 5px;
text-align: center;
font-weight: bold;
\}
</style>
app.svelte:
<script>
import Card from "./Card.svelte";
let studentData = \{
school: "Telebort Academy",
name: "John Doe",
studentID: "TB2024001",
year: "2024"
\};
</script>
<Card \{...studentData\} />
If you've done it correctly, it should look something like this:
✅ Make sure your Svelte Props projects include:
export let
💡 Tip
- Always use
export let
to create props in Svelte components- Props can be strings, numbers, or any JavaScript value
- Use object spread syntax
\{...obj\}
to pass multiple props efficiently- Calculated values can be derived from props within the same script section
- Test your components with different prop values to ensure they work correctly
Common Issues:
"Cannot read property of undefined"
Props not updating
export let
in the child componentSpread syntax not working
<Component {...objectName} />
Styling not appearing
<style>
section in your componentsℹ️ Ready to submit? Make sure you've completed all three activities and tested your components thoroughly!
When you have completed your "Svelte Props" projects, submit them using the link below:
Before submitting, verify:
⚠️ Warning Test all components with different prop values and verify they display correctly in the Svelte REPL before submitting!
Need help? Watch the Code Review: YouTube Tutorial