Learn Svelte Props

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…

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>

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!

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>

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

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.

What you will learn