MyComponent.svelte, we can receive props with the $props rune:
Signature
Destructuring props
More commonly, you’ll destructure your props:Fallback values
Destructuring allows us to declare fallback values, which are used if the parent component does not set a given prop (or the value isundefined):
Fallback values are not turned into reactive state proxies.
Renaming props
We can also use the destructuring assignment to rename props, which is necessary if they’re invalid identifiers, or a JavaScript keyword likesuper:
Rest props
Finally, we can use a rest property to get, well, the rest of the props:Updating props
References to a prop inside a component update when the prop itself updates — whencount changes in App.svelte, it will also change inside Child.svelte:
- App.svelte
- Child.svelte
Type safety
You can add type safety to your components by annotating your props. In TypeScript:Interfaces for native DOM elements are provided in the
svelte/elements module.children, these should be typed using the Snippet interface imported from 'svelte'.
Adding types is recommended, as it ensures that people using your component can easily discover which props they should provide.
$props.id()
for and aria-labelledby: