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.
Practice
{...props} do in a Svelte component?Solution
Step 1: Understand spread props syntax
The syntax{...props}takes all key-value pairs from thepropsobject.Step 2: Apply spread props in component
These key-value pairs are passed as individual props to the component, making them accessible inside.Final Answer:
It passes all properties from thepropsobject as individual props to the component. -> Option AQuick Check:
Spread props = pass all object keys as props [OK]
- Thinking spread props create a new component
- Confusing spread props with merging components
- Believing spread props remove props
user as props to a Svelte component <Profile />?Solution
Step 1: Recall spread props syntax in Svelte
Spread props use{...object}inside the component tag to pass all object keys as props.Step 2: Check correct self-closing tag syntax
In Svelte, self-closing tags use/>without extra characters or parentheses.Final Answer:
<Profile {...user} /> -> Option BQuick Check:
Correct spread syntax + self-closing tag =<Profile {...user} />[OK]
- Adding semicolons inside spread syntax
- Using parentheses after spread props
- Incorrectly closing the component tag
<script>
let data = {name: 'Alice', age: 30};
</script>
<UserCard {...data} age={25} />What will be the value of
age inside UserCard?Solution
Step 1: Understand spread props and explicit props order
The spread{...data}passesname: 'Alice'andage: 30first.Step 2: Recognize explicit prop override
The explicit propage={25}comes after spread and overrides the previousagevalue.Final Answer:
25 -> Option CQuick Check:
Explicit props override spread props [OK]
- Assuming spread props always override explicit props
- Thinking duplicate props cause errors
- Ignoring prop order in component tag
<script>
let props = {title: 'Hello'};
</script>
<Card {...props} title />Solution
Step 1: Check explicit prop syntax
The explicit proptitlewithout a value is treated astitle={true}in Svelte.Step 2: Understand Svelte prop override
This overrides the spreadtitle='Hello'totrue.Final Answer:
Explicit proptitlewithout value sets it totrue. -> Option DQuick Check:
Explicit props need values unless boolean [OK]
- Thinking spread and explicit props can't be combined
- Assuming missing value is allowed for any prop
- Believing self-closing tags are invalid here
<Button /> that accepts props type, disabled, and label. You want to pass an object btnProps with {type: 'submit', disabled: false} but override disabled to true and add label as 'Send'. Which is the correct usage?Solution
Step 1: Pass base props with spread first
Use{...btnProps}to passtypeanddisabled: false.Step 2: Override
Explicit props after spread override previous values, sodisabledand addlabelexplicitlydisabled={true}andlabel='Send'must come after.Step 3: Check order of props
Only<Button {...btnProps} disabled={true} label='Send' />places spread first, then explicit props, ensuring correct override.Final Answer:
<Button {...btnProps} disabled={true} label='Send' /> -> Option AQuick Check:
Spread first, explicit override after [OK]
- Placing explicit props before spread, losing override effect
- Duplicating props in wrong order
- Forgetting to add new props explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
