Component Events — Svelte

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…

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>

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

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} />

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>

⚠️ Why doesn't the news reach the parent?

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}.

What you will learn