0
0
Svelteframework~20 mins

Spread props in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spread Props Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Svelte component render?

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?

A&lt;button id=&quot;btn1&quot; class=&quot;primary&quot; disabled&gt;Click me&lt;/button&gt;
B&lt;button id=&quot;btn1&quot; class=&quot;primary&quot;&gt;Click me&lt;/button&gt;
C&lt;button disabled&gt;Click me&lt;/button&gt;
D&lt;button&gt;Click me&lt;/button&gt;
Attempts:
2 left
💡 Hint

Remember how boolean attributes like disabled behave in HTML.

📝 Syntax
intermediate
1:30remaining
Which spread syntax is correct in Svelte?

Which of the following is the correct way to spread props in a Svelte component?

A&lt;Component {props} /&gt;
B&lt;Component {...props} /&gt;
C&lt;Component ...props /&gt;
D&lt;Component {..props} /&gt;
Attempts:
2 left
💡 Hint

Think about how JavaScript spread syntax looks inside JSX-like attributes.

🔧 Debug
advanced
2:00remaining
Why does this spread cause an error?

Given this Svelte snippet:

<script>
  let props = null;
</script>

<input type="text" {...props} />

What error will this cause and why?

AReferenceError because props is undefined
BSyntaxError due to invalid spread syntax
CTypeError because you cannot spread null
DNo error, renders input without extra props
Attempts:
2 left
💡 Hint

Think about what spreading a non-object value does in JavaScript.

state_output
advanced
2:00remaining
What is the final class attribute value?

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?

Aprimary
Bbtn primary
Cbtn
Dbtnprimary
Attempts:
2 left
💡 Hint

Later spread props overwrite earlier ones.

🧠 Conceptual
expert
2:30remaining
How does Svelte handle spread props with reactive updates?

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?

AAn error occurs
BHello
Cundefined
DHi
Attempts:
2 left
💡 Hint

Reactive statements update variables and trigger re-render.