Sometimes you want a component to show itself inside itself. This helps when you have things like lists inside lists or trees. svelte:self lets a component call itself easily.
svelte:self for recursive components
<script> export let data; </script> {#if data.children} {#each data.children as child} <svelte:self data={child} /> {/each} {/if}
<svelte:self /> means the component calls itself.
You can pass props like normal to <svelte:self />.
<script> export let item; </script> <p>{item.name}</p> {#if item.children} <ul> {#each item.children as child} <li><svelte:self item={child} /></li> {/each} </ul> {/if}
<script> export let comment; </script> <div> <p>{comment.text}</p> {#if comment.replies} <div class="replies"> {#each comment.replies as reply} <svelte:self comment={reply} /> {/each} </div> {/if} </div>
This component shows a tree structure. It prints the node name and calls itself for each child node, adding indentation and a border to show levels.
<script> export let node; </script> <div style="margin-left: 1rem; border-left: 1px solid #ccc; padding-left: 0.5rem;"> <p>{node.name}</p> {#if node.children} {#each node.children as child} <svelte:self node={child} /> {/each} {/if} </div>
Using svelte:self is simple and avoids extra imports for recursive calls.
Be careful to have a stopping condition (like no children) to avoid infinite loops.
Time complexity depends on the size of the data tree you render.
svelte:self lets a component call itself inside its own template.
This is useful for showing nested or recursive data like trees or comments.
Always pass the right data and have a base case to stop recursion.
Practice
svelte:self in a Svelte component?Solution
Step 1: Understand the role of
svelte:selfsvelte:selfis a special tag that lets a component include itself inside its own template.Step 2: Identify the use case
This is mainly used for recursive rendering, like showing nested lists or trees.Final Answer:
To allow a component to render itself recursively. -> Option AQuick Check:
Recursive rendering = A [OK]
- Confusing
svelte:selfwith dynamic imports - Thinking it creates global state
- Assuming it styles components
svelte:self?Solution
Step 1: Recall the special tag syntax
The correct syntax to call the same component recursively is<svelte:self />.Step 2: Eliminate incorrect options
Other options use invalid or non-existent syntax in Svelte.Final Answer:
<svelte:self /> -> Option DQuick Check:
Correct recursive tag = C [OK]
<svelte:self /> for recursion [OK]- Using
Component.selfwhich is invalid - Omitting the
svelte:prefix - Trying to call
self()as a function
<script>
export let node;
</script>
<div>{node.name}</div>
{#if node.children}
<ul>
{#each node.children as child}
<li><svelte:self node={child} /></li>
{/each}
</ul>
{/if}What will be rendered if
node is:{"name": "Root", "children": [{"name": "Child1"}, {"name": "Child2"}]}Solution
Step 1: Understand the component structure
The component shows the current node's name, then if it has children, it renders a list with recursive calls for each child.Step 2: Apply the given data
Root node name is "Root" with two children "Child1" and "Child2". So it renders Root, then a list with Child1 and Child2 each inside a <div>.Final Answer:
<div>Root</div><ul><li><div>Child1</div></li><li><div>Child2</div></li></ul> -> Option CQuick Check:
Recursive tree renders nested divs and ul/li = A [OK]
{#each} with svelte:self [OK]- Ignoring the
{#if}condition for children - Assuming children render without list tags
- Thinking recursion causes syntax error here
<script>
export let node;
</script>
<div>{node.name}</div>
<svelte:self node={node.children[0]} />Assuming
node.children can be empty or undefined.Solution
Step 1: Analyze the recursive call
The component calls itself withnode.children[0]without checking if children exist or have elements.Step 2: Identify the problem
Ifnode.childrenis empty or undefined, the recursion never stops, causing infinite recursion or runtime errors.Final Answer:
It causes infinite recursion ifnode.childrenis empty or undefined. -> Option AQuick Check:
Missing base case or check = infinite recursion [OK]
svelte:self call [OK]- Assuming recursion stops automatically
- Ignoring undefined or empty children arrays
- Thinking syntax error occurs instead of runtime issue
svelte:self. Each comment has a text and optional replies array. Which approach correctly prevents infinite recursion and shows all comments?Solution
Step 1: Understand recursive rendering with base case
To avoid infinite recursion, only recurse ifrepliesexists and has items.Step 2: Apply recursion properly
Render the comment text, then use{#if replies && replies.length}and{#each replies as reply}with<svelte:self />passingreply.Final Answer:
Render comment text, then ifrepliesexists and is not empty, recursively rendersvelte:selffor each reply inside a list. -> Option BQuick Check:
Base case + recursive list = B [OK]
- Recursing without checking replies existence
- Ignoring replies and missing nested comments
- Trying to flatten replies in script instead of recursion
Ready to prove it?
Pass the test and this pattern moves unknown
