The Order button sits inside MenuItem, but the cart belongs to App. Props only flow down from parent to child, so how does MenuItem tell App that someone ordered? With a component event: news a child sends to its parent, such as "this dish was ordered". Props flow down, events flow up. If you come from Vue, this is emit. In React you usually pass a function through props, and that's also the way Svelte 5 goes.
Picture the bell on each table at Mrs. Sri's stall. A customer rings it and calls out "Fried Rice". The sound is only heard in the room where that bell hangs. The cashier in the front room hears it, but Mrs. Sri in the kitchen doesn't, unless the cashier passes the order on. If the room is empty, the bell still rings, nobody comes, and nobody complains.
Callback props: the child sends news
This is MenuItem.svelte from the Props lesson, now with an Order button:
<!-- MenuItem.svelte -->
<script>
let { name, price, onorder } = $props()
function order() {
onorder?.({ name, price })
}
</script>
<h3>{name}</h3>
<p>${price.toFixed(2)}</p>
<button onclick={order}>Order</button>
onorderis an ordinary prop holding a function from the parent. A prop that holds a function like this is called a callback prop. The name is up to you, but by habit it starts withon, likeonclick.onorder?.({ name, price })calls that function and sends the data as its argument. It can be any value, for exampleonsubmit?.({ data: value }).?.keeps the call silent when the parent sends noonorder.- MenuItem doesn't know who its parent is or what the function does. It only announces.
onorder: the parent gets the data
<!-- App.svelte -->
<script>
import MenuItem from "./MenuItem.svelte"
let cart = $state([])
function add(menu) {
cart.push(menu.name)
}
</script>
<p>Items in cart: {cart.length}</p>
<MenuItem name="Fried Rice" price={15} onorder={add} />
<MenuItem name="Iced Tea" price={5} onorder={add} />
| What happens | menu |
Shows |
|---|---|---|
| Click Order on Fried Rice | { name: "Fried Rice", price: 15 } |
Items in cart: 1 |
| Click Order on Iced Tea | { name: "Iced Tea", price: 5 } |
Items in cart: 2 |
onorder={add}is written likeonclickin the Event Handling lesson, but the name is your own. Behind the scenes, it's just sending a prop.addreceives the child's data directly as its argument, so the name is read asmenu.name. There's no event object and noevent.detail.onorder?.()with no argument makesmenuundefined, andmenu.namefails withTypeError: Cannot read properties of undefined (reading 'name').- Prop names match exactly, letter case too: a parent sending
onOrder={add}doesn't fill the child'sonorder, and clicking Order does nothing.
An event nobody listens to
<MenuItem name="Chicken Noodles" price={12} /> is written without onorder. Click its Order: onorder is undefined, and ?. skips the call. No error, no warning, not even in dev mode, and the cart doesn't change. So when a button click does nothing, first check that the parent really sends a prop with the same name. Without ?., that click ends in a TypeError instead, because onorder isn't a function.
Forwarding an event
Now the cards are wrapped in MenuList.svelte, and App only uses <MenuList onorder={add} />:
<!-- MenuList.svelte -->
<script>
import MenuItem from "./MenuItem.svelte"
let { onorder } = $props()
</script>
<MenuItem name="Fried Rice" price={15} {onorder} />
<MenuItem name="Chicken Noodles" price={12} {onorder} />
<MenuItem name="Iced Tea" price={5} {onorder} />
- App's function only reaches MenuList, the component App writes. It doesn't jump to MenuItem by itself. Without
{onorder}on each MenuItem,addin App never runs. {onorder}is shorthand foronorder={onorder}: MenuList takes the function from App and hands it down. This is forwarding in Svelte 5. App receives the same data,{ name, price }.- DOM events are passed on the same way.
<MenuItem onclick={logClick} />hears no click inside MenuItem, unless MenuItem takesonclickthrough$props()and calls it from its button too:<button onclick={(e) => { onclick?.(e); order() }}>.
Exercise: a button that calls onpress
In the exercise you write the child component, OrderButton.svelte:
<script>
let { onpress } = $props()
</script>
<button onclick={() => onpress?.({ message: "Hello!" })}>
Click me
</button>
The exercise editor holds only one component, so the parent is played by a small test file already in the editor (OrderButton.test.js). The test renders OrderButton with an onpress function, presses its button, then checks that the function received { message: "Hello!" }. That's why the prop's name must be exactly onpress, and the data exactly { message: "Hello!" }.
Older code: createEventDispatcher
In Svelte 4 tutorials and repos, MenuItem sends its news through createEventDispatcher:
<!-- MenuItem.svelte, Svelte 4 -->
<script>
import { createEventDispatcher } from "svelte"
export let name
export let price
const dispatch = createEventDispatcher()
</script>
<button on:click={() => dispatch("order", { name, price })}>Order</button>
dispatch("order", data)is whatonorder?.(data)is now.- The parent listens with
on:order={add}, and the data sits inevent.detail, not directly in the argument:event.detail.name. - Forwarding is written as a bare
on:orderin the middle component. Now the middle component takesonorderand hands it down. createEventDispatcheris deprecated in Svelte 5, though it still works. In new components, use a callback prop.
⚠️ Why doesn't the news reach the parent?
- A component in the middle must take
onorderand hand it down, or the news stops there with no error. onOrderandonorderare two different props, and?.keeps a typo like that silent.- A callback prop receives the data directly:
add(menu), thenmenu.name.menu.detailgivesundefined. - The exercise runs a test that sends
onpress, then checks your code for$propsandonclick, with none of the old way from the Older code section.
TL;DR: A child sends news to its parent through a callback prop: let { onorder } = $props(), then onorder?.({ name, price }). The parent sends the function like any prop, <MenuItem onorder={add} />, and add(menu) receives the data directly, with no event.detail. ?. keeps the call silent when the parent sends nothing, with no error. A parent's function doesn't jump past a component by itself, so a component in the middle takes it and hands it down: <MenuItem {onorder} />. Svelte 4 code uses the old way (see the Older code section). The exercise: OrderButton.svelte with let { onpress } = $props() and <button onclick={() => onpress?.({ message: "Hello!" })}>Click me</button>, tested by a test file that plays the parent.
⚡ A callback prop: the child sends news
Write:
<!-- MenuItem.svelte -->
<script>
let { name, price, onorder } = $props()
function order() {
onorder?.({ name, price })
}
</script>
<h3>{name}</h3>
<button onclick={order}>Order</button>
Meaning: onorder is an ordinary prop holding a function from the parent. The child calls it and sends the data as its argument. ?. keeps the call silent when the parent sends no onorder.
⚡ The parent gets the data directly
Write:
<script>
let cart = $state([])
function add(menu) {
cart.push(menu.name)
}
</script>
<MenuItem name="Fried Rice" price={15} onorder={add} />
Guess first: the parent writes onOrder={add}, with a capital O. Order is clicked. Does the cart grow?
No. onOrder and onorder are two different props, and ?. keeps a typo like that silent, with no error.
Meaning: add receives the child's data directly as its argument, so the name is read as menu.name. There's no event object and no event.detail.
⚡ A component in the middle passes it on
Write:
<!-- MenuList.svelte -->
<script>
import MenuItem from "./MenuItem.svelte"
let { onorder } = $props()
</script>
<MenuItem name="Fried Rice" price={15} {onorder} />
Meaning: App's function doesn't jump to MenuItem by itself. The component in the middle takes onorder, then hands it down: {onorder} is short for onorder={onorder}.