Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 35 ยท Components

Callback Props and $bindable

Send events from child to parent in Svelte 5 with callback props, expose two-way props with $bindable, and learn when each pattern fits.

Data flows down from parent to child through props. How does the child talk back? Svelte 5 has two answers: callback props (the child calls a function the parent gave it) and bindable props (the child and parent share one variable). This lesson explains both and when to choose each.

Callback props

A callback prop is just a function passed as a prop. By convention names start with on, matching DOM events:

SVELTE
<!-- Counter.svelte -->
<script>
  let { count = 0, onchange } = $props();
</script>

<button onclick={() => onchange?.(count + 1)}>
  {count}
</button>
SVELTE
<!-- Parent.svelte -->
<script>
  import Counter from './Counter.svelte';
  let total = $state(0);
</script>

<Counter count={total} onchange={(next) => (total = next)} />
<p>Total: {total}</p>

The child does not own the number. It reports what the user wants, and the parent decides. This keeps the data in one place, which makes bugs easier to find.

Replacing createEventDispatcher

Svelte 3/4 had createEventDispatcher and on:event listeners on components. That API is deprecated. Callback props are simpler, typed like any function, and can return values:

SVELTE
<!-- ConfirmButton.svelte -->
<script>
  let { onconfirm, label = 'Delete' } = $props();
</script>

<button onclick={() => confirm('Sure?') && onconfirm()}>{label}</button>

Forwarding DOM events

Because events are just props, wrappers forward them through rest props:

SVELTE
<script>
  let { children, ...rest } = $props();
</script>

<button {...rest}>{@render children?.()}</button>

<FancyButton onclick={save} onmouseenter={preview}> now works with no extra code.

$bindable: two-way props

Sometimes a child is really an input control for the parent's value (a custom text field, a date picker, a toggle). Writing value and onchange for each one is noisy. Mark the prop bindable, and the parent may use bind::

SVELTE
<!-- Toggle.svelte -->
<script>
  let { checked = $bindable(false) } = $props();
</script>

<button role="switch" aria-checked={checked} onclick={() => (checked = !checked)}>
  {checked ? 'On' : 'Off'}
</button>
SVELTE
<!-- Parent.svelte -->
<script>
  import Toggle from './Toggle.svelte';
  let notifications = $state(true);
</script>

<Toggle bind:checked={notifications} />
<p>Notifications are {notifications ? 'on' : 'off'}.</p>

The argument to $bindable() is the fallback value used when the parent passes nothing (no binding). Without the marker, the child cannot update the parent's variable.

Child talks to parent

Callback prop onchange(value)
Bindable prop bind:checked

Callbacks: parent decides. Bindable: shared variable.

1 / 2

Which one should you use?

Situation Prefer
Something happened (saved, deleted, selected) callback prop
Parent must validate or change the data first callback prop
A child is a form control for a parent's value $bindable
Many levels deep callback or shared state

Bindable props are convenient, but they let data change from two places. Use them for leaf form-like components, not for big state structures flowing through many layers.

Binding to element props

The same bind: syntax works with deeply nested object properties of state, and with bind:this on a component to get its exported functions:

SVELTE
<!-- Dialog.svelte -->
<script>
  let open = $state(false);
  export function show() { open = true; }
  export function hide() { open = false; }
</script>
SVELTE
<script>
  import Dialog from './Dialog.svelte';
  let dialog;
</script>

<Dialog bind:this={dialog} />
<button onclick={() => dialog.show()}>Open</button>

Exports like show are the way to expose imperative actions. Do this sparingly; state plus props is the normal route.

Stand-in: callbacks and shared variables

The first example shows a parent that owns state and a child that reports changes. The second shows a "binding" as a get/set pair, the idea behind bind:.

JS Callback props (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Binding as a getter/setter pair (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Common mistakes

  • Forgetting $bindable() and then wondering why bind: warns or the parent never updates.
  • Mutating a non-bindable object prop from the child; Svelte warns because it breaks one-way flow.
  • Calling the callback unconditionally when it is optional: use onchange?.().

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What replaces createEventDispatcher in Svelte 5?

    Pass a function as a prop (conventionally named onsomething) and call it.
  2. 2. What must a child do so that its parent can use bind:checked?

    let { checked = $bindable(false) } = $props(); allows two-way binding.
  3. 3. What is the argument to $bindable(false)?

    It is the default when no binding or value is provided.
  4. 4. Which case favors a callback prop over $bindable?

    Callbacks let the parent decide what to do with the information.

Summary

  • Callback props (onchange, onconfirm) let children notify parents without owning the data.
  • $bindable() marks a prop as two-way so the parent can use bind:.
  • Use bindable props for leaf form controls; prefer callbacks for events and validation.
  • createEventDispatcher is deprecated; exported functions with bind:this expose imperative actions.

Next: Shared State in Modules