All three <MenuItem /> from the previous lesson were "Fried Rice". What gives each copy its own content is props: data a parent component sends to a child component, much like arguments sent to a function. The parent component is the one that places another component (like App), and the child component is the one being placed (like MenuItem). One piece of data in the props is called a prop. If you have used React, these are the props you receive in function MenuItem({ name }). In Vue, props are registered with defineProps. Svelte takes props from $props(), in a shape much like React's.
Picture the order slip at Mrs. Sri's stall, printed with "Welcome, ____!". The slip is the same for every buyer, and the cashier writes the buyer's name on the blank line. The cashier may also cross out the printed "Welcome" and write a different greeting. The component is the printed slip, a prop is what the cashier fills in (the name, or a new greeting), and a default value is the greeting already printed. The cashier who fills in the slip is the parent component.
Declaring a prop with $props
In Svelte 5, a component takes its props from $props(). This is Greeting.svelte:
<script>
let { name, message = "Welcome" } = $props()
</script>
<p>{message}, {name}!</p>
$props()returns every prop the parent sent, in one object. The curly braces left of=unpack that object: each property is taken out into its own variable. This is called destructuring. Sonameandmessagebecome variables right away.namehas no starting value, so the parent is expected to always send it. A prop like this is called a required prop.message = "Welcome"gives the propmessagea default value. If the parent doesn't sendmessage,"Welcome"is used.$propsis a rune, a special word starting with$that the compiler recognises.$props()needs no import. Call it directly in the component's<script>, not inside a function.- Without
$props(),let nameis just a plain variable belonging to that component, and the parent can't fill it.
Sending props from the parent
In the parent component, props are sent like HTML attributes:
<script>
import Greeting from "./Greeting.svelte"
</script>
<Greeting name="Budi" />
<Greeting name="Sari" message="How are you" />
| The parent writes | Shows |
|---|---|
<Greeting name="Budi" /> |
Welcome, Budi! |
<Greeting name="Sari" message="How are you" /> |
How are you, Sari! |
- The first line doesn't send
message, so its default shows. The second line sendsmessage, and that value replaces the default. - A value in quotes is always a string. Numbers, variables and expressions are sent through curly braces.
Now the stall's menu cards can differ. MenuItem.svelte takes two props:
<script>
let { name, price } = $props()
</script>
<h3>{name}</h3>
<p>{price.toLocaleString("en-US", { style: "currency", currency: "USD" })}</p>
The parent places three cards, each with its own content:
<MenuItem name="Fried Rice" price={15} />
<MenuItem name="Chicken Noodles" price={12} />
<MenuItem name="Iced Tea" price={5} />
Three cards, three contents: Fried Rice $15.00, Chicken Noodles $12.00, Iced Tea $5.00. price={5} sends a number. price="5" sends a string, and toLocaleString doesn't format a string. So it shows 5, not $5.00.
A prop that isn't sent
Suppose the parent writes <Greeting />, sending nothing. message has a default, name doesn't. Before you look at the table, take a guess: does it show an error, Welcome, !, or Welcome, undefined!?
| Prop | Its value | What shows |
|---|---|---|
message |
the default "Welcome" |
Welcome |
name |
undefined |
nothing |
The result is Welcome, !. No error, and no warning in the console either, not even during npm run dev. Svelte 5 shows undefined as empty text, so a prop you forgot to send is easy to miss. The sign: the comma sitting right against the exclamation mark.
A prop that isn't sent can cause worse problems than empty text. <MenuItem name="Iced Tea" /> without price makes price.toLocaleString(...) fail, because price holds undefined. The error is TypeError: Cannot read properties of undefined (reading 'toLocaleString'), and the app stops at that error.
In the second exercise, your component is deliberately run with no parent sending anything. So its paragraph shows Welcome, !, and that's right: the empty name doesn't count as wrong, and what gets checked is that the default Welcome really shows.
Spread: send everything at once
When the data is already in one object, send all of it with spread, meaning three dots in front of the object's name:
<script>
import Greeting from "./Greeting.svelte"
const customer = { name: "Sari", message: "How are you" }
</script>
<Greeting {...customer} />
{...customer} is the same as writing name="Sari" message="How are you". Each property of the object becomes one prop with the same name. There is also a shorthand when a variable has the same name as the prop. It works like the attribute shorthand in Svelte Template Basics: <Greeting {name} /> is the same as <Greeting name={name} />.
Older code: export let
In Svelte 4 tutorials and repos, the same Greeting.svelte is written like this:
<script>
export let name
export let message = "Welcome"
</script>
<p>{message}, {name}!</p>
- Each
export letis one name insidelet { ... } = $props(). Theexportthere is not a JavaScript module export. Svelte 4 only borrowed the word to say "this variable is filled from outside". - Props that the parent sent but that weren't declared (not written with
export let) are read through$$restProps. In Svelte 5 it'slet { name, ...rest } = $props(), andrestholds all the other props. - Svelte 4 shows a required prop that wasn't sent as the word
undefined(Welcome, undefined!), plus a warning in dev mode. export letand$props()can't be mixed. When a component uses a rune, the compiler rejectsexport letwith the compile errorlegacy_export_invalid.
The parent sends props exactly the same way in both versions. So when you move Svelte 4 code to Svelte 5, only the child component's <script> needs changing.
⚠️ Why the prop doesn't arrive right
- Forgetting
$props(), or its curly braces. A plainlet namecan't be filled from outside: the parent sendsname="Budi", but nothing shows.let name = $props()without curly braces holds the whole props object instead, so it shows[object Object]. Writelet { name } = $props(). This is exactly what the first exercise checks. - Typing the name straight into the child.
<h2>Hello, Budi!</h2>doesn't read the prop, so every copy greets Budi. Write<h2>Hello, {name}!</h2>. - A number in quotes.
price="5"sends a string. Send a number withprice={5}. - Mixing the two versions' styles. The compiler rejects code from a Svelte 4 tutorial (see the Older code section) when that code is mixed with
$props()or another rune in one component. Turn all of it into$props().
TL;DR: A prop is data that a parent component (the one that places another component) sends to a child component (the one being placed). In Svelte 5 the child takes it with the $props() rune (a rune is a special Svelte 5 word starting with $): let { name, message = "Welcome" } = $props(). name has no starting value, so it must be sent. message has a default, used when the parent doesn't send it. The parent sends props like attributes: <Greeting name="Budi" /> for a string, price={5} for a number, or {...customer} for everything in the object customer at once. A required prop that isn't sent shows as empty (Welcome, !), with no error and no warning. If your code reads a property or method of that prop, such as price.toLocaleString(), the result is a TypeError. Svelte 4 code writes props the old way (export let), and that way can't be mixed with $props() in one component.
⚡ The child takes props from $props
Write:
<script>
let { name, message = "Welcome" } = $props()
</script>
<p>{message}, {name}!</p>
Meaning: This is the component Greeting.svelte. A prop is data that a parent component (the one placing Greeting) sends to a child component (Greeting itself). $props() returns every prop in one object, and the curly braces left of = unpack it into the variables name and message. name must be sent, while message has the default "Welcome", used when the parent doesn't send it.
⚡ The parent sends them like attributes
Write:
<Greeting name="Budi" />
<Greeting name="Sari" message="How are you" />
Meaning: The parent sends props to Greeting like HTML attributes. The first line shows Welcome, Budi!, the second How are you, Sari!. A value in quotes is always a string, so a number is sent through curly braces, for example price={5}.
⚡ A prop that isn't sent
Guess first: the parent writes <Greeting />, sending nothing. Does it show an error, Welcome, !, or Welcome, undefined!?
Welcome, !. No error, and no warning either. Svelte 5 shows undefined as empty text.
Meaning: A required prop such as name that you forgot to send is easy to miss, because there is no error or warning. The sign: the comma sitting right against the exclamation mark.