0
0
Svelteframework~20 mins

svelte:self for recursive components - Practice Problems & Coding Challenges

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

Consider a Svelte component that uses <svelte:self> to render a nested list recursively. What will be the rendered output for the given data?

Svelte
<script>
  export let node;
</script>
<ul>
  <li>{node.name}</li>
  {#if node.children}
    <ul>
      {#each node.children as child}
        <svelte:self node={child} />
      {/each}
    </ul>
  {/if}
</ul>
A<ul><li>Root</li><li>Child 1</li><li>Child 2</li><li>Grandchild</li></ul>
B<ul><li>Root</li><ul><ul><li>Child 1</li></ul><ul><li>Child 2</li><ul><ul><li>Grandchild</li></ul></ul></ul></ul></ul>
C<ul><li>Root</li><ul><li>Child 1</li><li>Child 2</li><ul><li>Grandchild</li></ul></ul></ul>
D<ul><li>Root</li></ul>
Attempts:
2 left
💡 Hint

Think about how <svelte:self> calls the same component recursively for each child node.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses <svelte:self> for recursion?

Which of the following Svelte snippets correctly uses <svelte:self> to recursively render a tree structure?

A<ul>{#each items as item}<li>{item.name}{#if item.children}<svelte:self node={item.children[0]} />{/if}</li>{/each}</ul>
B<ul>{#each items as item}<li>{item.name}<svelte:self items={item.children} /></li>{/each}</ul>
C<ul>{#each items as item}<li>{item.name}{#if item.children}<svelte:self node={item} />{/if}</li>{/each}</ul>
D<ul>{#each items as item}<li>{item.name}{#if item.children}<svelte:self node={item.children} />{/if}</li>{/each}</ul>
Attempts:
2 left
💡 Hint

Remember that <svelte:self> calls the same component and you must pass a single node, not an array, for recursion.

🔧 Debug
advanced
2:00remaining
Why does this recursive Svelte component cause a maximum call stack error?

Given this Svelte component using <svelte:self>, why does it crash with a maximum call stack exceeded error?

Svelte
<script>
  export let node;
</script>
<ul>
  <li>{node.name}</li>
  {#if node.children}
    {#each node.children as child}
      <svelte:self node={node} />
    {/each}
  {/if}
</ul>
AThe <code>{#each}</code> block syntax is incorrect.
BIt uses <code>&lt;svelte:self&gt;</code> inside a loop which is not allowed.
CThe <code>node</code> prop is missing causing undefined errors.
DIt passes the same node repeatedly causing infinite recursion.
Attempts:
2 left
💡 Hint

Check what is passed to <svelte:self> inside the loop.

state_output
advanced
2:00remaining
What is the value of count after clicking all buttons in this recursive Svelte component?

This component renders buttons recursively using <svelte:self>. Each button increments a shared count state. What is the final count after clicking all buttons once?

Svelte
<script>
  import { writable } from 'svelte/store';
  export let node;
  const count = writable(0);
  function increment() {
    count.update(n => n + 1);
  }
</script>
<div>
  <button on:click={increment}>{node.name}</button>
  {#if node.children}
    {#each node.children as child}
      <svelte:self node={child} />
    {/each}
  {/if}
  <p>Count: {$count}</p>
</div>
AThe count equals the number of leaf nodes only.
BThe count equals the total number of nodes in the tree.
CThe count remains 0 because state is local to each component instance.
DThe count equals the number of root nodes only.
Attempts:
2 left
💡 Hint

Consider that count is a shared store imported once and used by all instances.

🧠 Conceptual
expert
2:00remaining
Why use <svelte:self> instead of importing the component for recursion?

In Svelte, what is the main advantage of using <svelte:self> for recursive components instead of importing the component by name and calling it recursively?

A<code>&lt;svelte:self&gt;</code> avoids circular import issues and simplifies recursion within the same component file.
BUsing <code>&lt;svelte:self&gt;</code> improves runtime performance by caching the component instance.
CIt automatically memoizes the component to prevent unnecessary re-renders.
DIt allows passing props without explicitly declaring them in the component.
Attempts:
2 left
💡 Hint

Think about how importing a component inside itself can cause problems.