Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 35 ยท Components

Component Props

Pass data into Svelte 5 components with the $props rune: defaults, renaming, rest props, spreading, TypeScript types and why props are reactive.

Components become useful when you can configure them. A Button needs a label, a Card needs a title, a UserList needs users. In Svelte 5, a component receives its inputs, called props, through the $props rune.

Declaring props

SVELTE
<!-- Greeting.svelte -->
<script>
  let { name, excited = false } = $props();
</script>

<p>Hello {name}{excited ? '!' : '.'}</p>

Use it:

SVELTE
<script>
  import Greeting from './Greeting.svelte';
</script>

<Greeting name="Ada" />
<Greeting name="Grace" excited />

$props() returns an object, and you almost always destructure it. A default value (excited = false) applies when the parent passes nothing, or passes undefined.

Older Svelte wrote export let name;. That form still compiles in "legacy mode", but in Svelte 5 components using runes should use $props().

Passing values

SVELTE
<Card title="Static string" />
<Card title={dynamicTitle} />
<Card {title} />                    <!-- shorthand -->
<Card {...cardProps} />             <!-- spread an object -->
<Card count={5} tags={['a', 'b']} /> <!-- any JavaScript value -->

Strings in quotes stay strings; everything else goes in braces. A bare attribute (excited) means true.

Props are reactive

When the parent's state changes, the child receives the new value:

SVELTE
<!-- Parent.svelte -->
<script>
  import Counter from './Counter.svelte';
  let n = $state(1);
</script>

<button onclick={() => n++}>Bump</button>
<Counter value={n} />

<!-- Counter.svelte -->
<script>
  let { value } = $props();
  let doubled = $derived(value * 2);
</script>

<p>{value} doubled is {doubled}</p>

Use $derived for values computed from props. A plain let doubled = value * 2 would compute once and never update.

Props are not yours to mutate

Treat props as read-only inputs. Reassigning a prop (value = 5) works locally but gets overwritten the next time the parent updates it. Mutating an object prop that the parent owns (user.name = 'x') is possible if it is deep state, but Svelte warns because it hides data flow. If a child needs to change something, give it a callback (see callbacks and bindable props).

If a child needs its own editable copy that starts from the prop, say so explicitly:

SVELTE
<script>
  let { initial } = $props();
  let draft = $state(initial); // initial value only; later changes are not copied
</script>

The compiler may warn about reading a prop in a non-reactive position; that is it checking you meant "initial value only".

Renaming and reserved words

Destructuring lets you rename:

SVELTE
<script>
  let { class: className, for: htmlFor } = $props();
</script>

<label class={className} for={htmlFor}><slot /></label>

class and for are reserved words in JavaScript, so you rename them.

Rest props

Collect the props you did not name and forward them to an element:

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

<button class={variant} {...rest}>
  Press
</button>

<Button variant="danger" disabled onclick={remove} /> now passes disabled and onclick through to the real button. This is the standard way to build thin wrappers over native elements.

TypeScript

With TypeScript, annotate the destructured object:

SVELTE
<script lang="ts">
  interface Props {
    name: string;
    excited?: boolean;
    onselect?: (id: number) => void;
  }

  let { name, excited = false, onselect }: Props = $props();
</script>

Svelte also ships helper types such as HTMLButtonAttributes from svelte/elements, handy for wrappers that accept every attribute of a native element.

The $props.id() helper

Need a unique id for a form control and its label? $props.id() returns an id unique to the component instance, stable between server and client:

SVELTE
<script>
  const uid = $props.id();
</script>

<label for="{uid}-email">Email</label>
<input id="{uid}-email" type="email" />

Stand-in: props as an object

A component is a function of its props. This stand-in shows defaults, rest forwarding and recomputation when the parent changes a value.

JS Defaults and rest props (stand-in)

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

No output.

A prop-driven component (stand-in page)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which line reads props in Svelte 5 runes mode?

    $props() returns the props object, normally destructured.
  2. 2. What does ...rest collect?

    Rest props let you forward unknown attributes to an element or child component.
  3. 3. How should you derive a value from a prop?

    $derived recomputes when the prop changes; a plain let would compute only once.
  4. 4. Why rename class when destructuring props?

    You cannot declare a variable named class, so write { class: className }.

Summary

  • Receive props with let { a, b = default, ...rest } = $props();.
  • Pass props as attributes, shorthand or spread {...obj}.
  • Props are reactive inputs; derive from them with $derived and do not reassign them.
  • Rest props forward unknown attributes to native elements.
  • Type props with an interface; $props.id() gives a unique instance id.

Next: Snippets and Children