Spread props let you pass many properties to a component quickly. It saves time and keeps your code clean.
Spread props in Svelte
<Component {...props} />The {...props} syntax spreads all key-value pairs from the props object as individual props.
You can combine spread props with other props. Later props override earlier ones.
buttonProps object to the Button component.<Button {...buttonProps} />inputProps but explicitly sets type to "text". The explicit type overrides any type in inputProps.<Input {...inputProps} type="text" />title and content props.<Card {...{title: 'Hello', content: 'World'}} />This example shows how to pass multiple props from an object props to a Child component using spread props. The child receives each property as a separate prop and displays them.
<script> import Child from './Child.svelte'; let props = { name: 'Alice', age: 30, city: 'Wonderland' }; </script> <Child {...props} /> <!-- Child.svelte --> <script> export let name; export let age; export let city; </script> <p>Name: {name}</p> <p>Age: {age}</p> <p>City: {city}</p>
Spread props make your code shorter but be careful not to pass unwanted props.
If you spread props and also set the same prop explicitly, the explicit one wins.
Use spread props to forward all props from a parent to a child component easily.
Spread props let you pass many props from an object quickly.
You can combine spread props with explicit props; explicit ones override.
They help keep your component calls clean and flexible.