0
0
Svelteframework~20 mins

svelte:component for dynamic components - Practice Problems & Coding Challenges

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

Given the following Svelte code using <svelte:component>, what will be displayed on the page?

Svelte
<script>
  import Button from './Button.svelte';
  import Link from './Link.svelte';
  let current = Button;
</script>

<svelte:component this={current} text="Click me" />
AA link with the label 'Click me' is shown.
BA button with the label 'Click me' is shown.
CNothing is shown because 'current' is not a string.
DAn error occurs because 'this' attribute is missing.
Attempts:
2 left
💡 Hint

Look at the value of current and which component it points to.

📝 Syntax
intermediate
1:30remaining
Which option correctly uses <svelte:component> to render a dynamic component?

Choose the correct syntax to render a dynamic component stored in the variable componentVar with a prop title set to 'Hello'.

A<svelte:component this="componentVar" title="Hello" />
B<svelte:component component={componentVar} title="Hello" />
C<svelte:component this={componentVar} title="Hello" />
D<svelte:component this={componentVar} {title="Hello"} />
Attempts:
2 left
💡 Hint

The this attribute expects a component reference, not a string or a different attribute name.

state_output
advanced
2:00remaining
What is the output after clicking the button twice?

Consider this Svelte component that switches between two components dynamically on button click. What text is shown after clicking the button twice?

Svelte
<script>
  import A from './A.svelte';
  import B from './B.svelte';
  let current = A;
  function toggle() {
    current = current === A ? B : A;
  }
</script>

<button on:click={toggle}>Switch</button>
<svelte:component this={current} />
AThe component A is shown.
BAn error occurs because toggle is not bound.
CNo component is shown because current is undefined.
DThe component B is shown.
Attempts:
2 left
💡 Hint

Think about how the toggle function changes current each click.

🔧 Debug
advanced
2:00remaining
Why does this code cause a runtime error?

Examine the code below. Why does it cause a runtime error when rendered?

Svelte
<script>
  let componentName = 'MyComponent';
</script>

<svelte:component this={componentName} />
ABecause the component name must be lowercase.
BBecause <code>componentName</code> is not imported.
CBecause <code>svelte:component</code> cannot be used without props.
DBecause <code>this</code> expects a component, not a string variable.
Attempts:
2 left
💡 Hint

Check the type of value passed to this in <svelte:component>.

🧠 Conceptual
expert
2:30remaining
How does <svelte:component> improve component reuse?

Which statement best explains the benefit of using <svelte:component> for dynamic components?

AIt allows rendering different components dynamically without writing multiple conditional blocks.
BIt automatically optimizes component rendering for better performance.
CIt replaces the need for importing components explicitly in the script.
DIt enables components to be styled globally without scoped CSS.
Attempts:
2 left
💡 Hint

Think about how dynamic rendering reduces repetitive code.