Consider this Svelte component using spread props:
<script>
let props = { id: 'btn1', class: 'primary', disabled: true };
</script>
<button {...props}>Click me</button>What will be the rendered output in the browser?
Remember how boolean attributes like disabled behave in HTML.
The spread operator passes all properties from props as attributes. The disabled attribute is boolean and appears without a value when true.
Which of the following is the correct way to spread props in a Svelte component?
Think about how JavaScript spread syntax looks inside JSX-like attributes.
The correct syntax uses three dots inside curly braces: {...props}. Other options are invalid in Svelte.
Given this Svelte snippet:
<script>
let props = null;
</script>
<input type="text" {...props} />What error will this cause and why?
Think about what spreading a non-object value does in JavaScript.
Spreading expects an object. Spreading null causes a TypeError at runtime.
Look at this Svelte component:
<script>
let base = { class: 'btn' };
let extra = { class: 'primary' };
</script>
<button {...base} {...extra}>Press</button>What will be the value of the class attribute on the button?
Later spread props overwrite earlier ones.
The second spread {...extra} overwrites the class from {...base}, so only 'primary' remains.
Consider this Svelte code:
<script>
let props = { title: 'Hello' };
$: props = { ...props, title: 'Hi' };
</script>
<h1 {...props}>Welcome</h1>What will be the rendered title attribute on the <h1> element after reactive update?
Reactive statements update variables and trigger re-render.
The reactive statement updates props to have title: 'Hi'. The spread uses the latest props, so the attribute is 'Hi'.