0
0
Svelteframework~20 mins

Why special elements handle edge cases in Svelte - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Special Elements Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does the element behave with dynamic components?
Consider the following Svelte code using . What will be rendered when current is set to Button component?
Svelte
<script>
  import Button from './Button.svelte';
  import Label from './Label.svelte';
  let current = Button;
</script>

<svelte:component this={current} />
AThe Label component is rendered regardless of the value of current.
BNothing is rendered because <svelte:component> requires a string, not a component.
CAn error is thrown because <svelte:component> cannot accept variables.
DThe Button component is rendered in place of <svelte:component>.
Attempts:
2 left
💡 Hint
Think about how dynamically renders the component passed to it.
lifecycle
intermediate
2:00remaining
What happens when listens to events?
Given this Svelte snippet, what is the effect of the element?
Svelte
<script>
  let width = 0;
  function updateWidth() {
    width = window.innerWidth;
  }
</script>

<svelte:window on:resize={updateWidth} />
<p>Window width: {width}</p>
AThe code throws an error because on:resize is invalid on <svelte:window>.
BThe width never updates because <svelte:window> cannot listen to events.
CThe width updates whenever the browser window is resized.
DThe width updates only once when the component mounts.
Attempts:
2 left
💡 Hint
Consider what does with event listeners.
📝 Syntax
advanced
2:00remaining
What happens if wraps a {#each} block?
Examine this code snippet. What will happen?
Svelte
<script>
  let items = [1, 2, 3];
</script>

<svelte:fragment>
  {#each items as item}
    <p>{item}</p>
  {/each}
</svelte:fragment>
ANo error; the fragment renders all items as paragraphs.
BSyntaxError: <svelte:fragment> must be inside another component or block.
CRuntimeError: Cannot use #each inside <svelte:fragment>.
DTypeError: items is not iterable.
Attempts:
2 left
💡 Hint
is designed to wrap Svelte blocks like {#each} without adding a DOM node.
🔧 Debug
advanced
2:00remaining
How does recursive behave here?
This component uses inside its markup. What best describes the behavior?
Svelte
<script>
  export let count = 0;
</script>

{#if count < 3}
  <svelte:self count={count + 1} />
{/if}
<p>Count: {count}</p>
AThe component recursively renders itself until count reaches 3, causing multiple nested renders.
BThe component does not render anything because <svelte:self> is invalid.
CThe component renders only once because count is not reactive.
DThe component throws a syntax error because <svelte:self> cannot have props.
Attempts:
2 left
💡 Hint
Think about how recursion works with and the count prop.
🧠 Conceptual
expert
3:00remaining
Why does handle edge cases better than manual event listeners?
Which reason best explains why using for global events is preferred over adding event listeners manually in onMount?
ABecause <svelte:window> only works with resize events, making it simpler.
BBecause <svelte:window> automatically cleans up event listeners when the component unmounts, preventing memory leaks.
CBecause manual event listeners cannot access window properties inside Svelte components.
DBecause <svelte:window> runs event handlers outside the Svelte reactivity system, improving performance.
Attempts:
2 left
💡 Hint
Think about lifecycle and cleanup of event listeners.