Skip to main content
Props and state are the fundamental ways to manage data in Svelte components. Props pass data from parent to child, while state manages data within a component.

Component Props

Props (short for properties) are how you pass data to components. You pass props just like you pass attributes to elements:

Declaring Props with $props

Inside the child component, receive props with the $props rune:
1
Destructuring Props
2
More commonly, you’ll destructure your props:
3
<script>
  let { adjective, count } = $props();
</script>

<p>This component is {adjective}</p>
<p>Count: {count}</p>
4
Fallback Values
5
Provide default values for props that may not be passed:
6
<script>
  let { adjective = 'happy', count = 0 } = $props();
</script>
7
Renaming Props
8
Use destructuring assignment to rename props:
9
<script>
  let { class: className, super: trouper = 'lights are gonna find me' } = $props();
</script>
10
Rest Props
11
Capture remaining props with a rest property:
12
<script>
  let { title, description, ...others } = $props();
</script>

<div {...others}>
  <h1>{title}</h1>
  <p>{description}</p>
</div>

Type-Safe Props

Add type safety using TypeScript:
Or using JSDoc:

Component State

The $state rune creates reactive state within a component:

Deep Reactive State

Arrays and objects become deeply reactive state proxies:

State in Classes

Use $state in class fields:

Derived State

Create computed values with the $derived rune:
For complex derivations, use $derived.by:

Bindable Props

Create two-way bindings with $bindable:

Updating Props

References to props update automatically when the prop changes. You can temporarily reassign props:
However, avoid mutating props unless they are bindable. Use callback props or $bindable for parent-child communication.

Unique IDs with $props.id()

Generate unique IDs for component instances:

Raw State

For non-reactive objects, use $state.raw:

State Snapshots

Take static snapshots of reactive state: