Bird
Raised Fist0
Svelteframework~20 mins

Lifecycle in nested components in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Svelte Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
Understanding lifecycle order in nested Svelte components

Consider a parent Svelte component that includes a child component. Both use onMount lifecycle functions that log messages. What is the order of console logs when the parent component is first rendered?

Svelte
<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  import { onMount } from 'svelte';
  onMount(() => console.log('Parent mounted'));
</script>
<Child />

<!-- Child.svelte -->
<script>
  import { onMount } from 'svelte';
  onMount(() => console.log('Child mounted'));
</script>
<p>Child content</p>
ABoth logs appear simultaneously
B"Parent mounted" logged first, then "Child mounted"
C"Child mounted" logged first, then "Parent mounted"
DNo logs appear because onMount is not called in nested components
Attempts:
2 left
💡 Hint

Remember that child components mount before their parents finish mounting.

❓ lifecycle
intermediate
2:00remaining
When does onDestroy run in nested components?

If a parent Svelte component is removed from the DOM, when do the onDestroy lifecycle functions of its nested child components run?

Svelte
<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  import { onDestroy } from 'svelte';
  onDestroy(() => console.log('Parent destroyed'));
</script>
{#if showChild}
  <Child />
{/if}

<!-- Child.svelte -->
<script>
  import { onDestroy } from 'svelte';
  onDestroy(() => console.log('Child destroyed'));
</script>
<p>Child content</p>
AOnly Parent's onDestroy runs, Child's does not
BChild's onDestroy runs before Parent's onDestroy
CParent's onDestroy runs before Child's onDestroy
DNeither onDestroy runs automatically
Attempts:
2 left
💡 Hint

Think about the order components are torn down in the DOM.

🔧 Debug
advanced
2:00remaining
Why does a nested component's onMount not run as expected?

A developer notices that a nested Svelte component's onMount callback never logs anything, even though the component renders. What is the most likely cause?

Svelte
<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  let showChild = false;
  setTimeout(() => showChild = true, 1000);
</script>
{#if showChild}
  <Child />
{/if}

<!-- Child.svelte -->
<script>
  import { onMount } from 'svelte';
  onMount(() => console.log('Child mounted'));
</script>
<p>Child content</p>
AThe Child component is never added to the DOM because showChild stays false
BThe onMount callback is inside a <script> tag but not imported correctly
CThe onMount callback runs only once and the console is cleared before logging
DThe Child component is added after 1 second, so onMount runs after delay
Attempts:
2 left
💡 Hint

Check when the Child component appears in the DOM.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in nested component lifecycle usage

Which option contains a syntax error in using onMount inside a nested Svelte component?

Svelte
<!-- Child.svelte -->
<script>
  import { onMount } from 'svelte';
  onMount(() => {
    console.log('Mounted');
  })
</script>
<p>Child content</p>
AMissing closing brace for the arrow function block
BMissing import statement for onMount
CUsing onMount inside <style> tag instead of <script>
DUsing onMount without parentheses
Attempts:
2 left
💡 Hint

Check the parentheses and braces in the callback function.

❓ state_output
expert
3:00remaining
How does state update affect lifecycle in nested Svelte components?

Given a parent component that passes a prop to a child component, and the child uses onMount to log the prop value, what happens if the parent updates the prop after mount?

Svelte
<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  import { onMount } from 'svelte';
  let count = 0;
  onMount(() => {
    setTimeout(() => count = 1, 1000);
  });
</script>
<Child value={count} />

<!-- Child.svelte -->
<script>
  import { onMount } from 'svelte';
  export let value;
  onMount(() => console.log('Mounted with value:', value));
  $: console.log('Value changed to:', value);
</script>
<p>Value is {value}</p>
AChild logs 'Mounted with value: 0' once, then logs 'Value changed to: 1' after update
BChild logs nothing because onMount does not run with props
CChild only logs 'Value changed to: 1' after update, no onMount log
DChild logs 'Mounted with value: 0' and 'Mounted with value: 1' after update
Attempts:
2 left
💡 Hint

Remember that onMount runs once, but reactive statements run on prop changes.

Practice

(1/5)
1. In Svelte, when you have nested components, which lifecycle method runs first in the child component after it is added to the DOM?
easy
A. onMount
B. onDestroy
C. beforeUpdate
D. afterUpdate

Solution

  1. Step 1: Understand lifecycle order in nested components

    When a child component is added to the DOM, its onMount runs after the component is rendered.
  2. Step 2: Identify the first lifecycle method after mounting

    onMount is the first lifecycle method called after the component is inserted, before any updates or destruction.
  3. Final Answer:

    onMount -> Option A
  4. Quick Check:

    Child component mounts first with onMount [OK]
Hint: onMount runs first when a component appears [OK]
Common Mistakes:
  • Confusing onDestroy as first method
  • Thinking beforeUpdate runs before onMount
  • Assuming afterUpdate runs on initial mount
2. Which of the following is the correct way to import and use onMount in a nested Svelte component?
easy
A. import { onMount } from 'svelte/lifecycle'; onMount(() => { console.log('Mounted'); });
B. import onMount from 'svelte'; onMount(() => { console.log('Mounted'); });
C. import { onMount } from 'svelte'; onMount(() => { console.log('Mounted'); });
D. import { onMount } from 'svelte'; onMount = () => { console.log('Mounted'); };

Solution

  1. Step 1: Check correct import syntax

    The correct import is import { onMount } from 'svelte'; using named import from 'svelte'.
  2. Step 2: Verify usage of onMount function

    Calling onMount(() => { ... }) registers the callback properly. Assigning onMount to a function is incorrect.
  3. Final Answer:

    import { onMount } from 'svelte'; onMount(() => { console.log('Mounted'); }); -> Option C
  4. Quick Check:

    Named import and callback usage [OK]
Hint: Use named import from 'svelte' for lifecycle hooks [OK]
Common Mistakes:
  • Using default import instead of named import
  • Importing from wrong path like 'svelte/lifecycle'
  • Assigning onMount instead of calling it
3. Consider a parent component that includes a child component. Both have onMount hooks that log messages. What is the order of console logs when the parent is rendered?
medium
A. Child's onMount logs first, then parent's onMount
B. Parent's onMount logs first, then child's onMount
C. Both onMount logs happen simultaneously
D. No logs appear because onMount doesn't run in nested components

Solution

  1. Step 1: Understand mounting order in nested components

    Child components mount before their parents finish mounting, so child's onMount runs first.
  2. Step 2: Confirm console log order

    Child's onMount logs first, then parent's onMount logs after child is fully mounted.
  3. Final Answer:

    Child's onMount logs first, then parent's onMount -> Option A
  4. Quick Check:

    Child mounts before parent finishes [OK]
Hint: Child onMount runs before parent onMount [OK]
Common Mistakes:
  • Assuming parent mounts before child
  • Thinking onMount runs simultaneously
  • Believing onMount doesn't run in nested components
4. You have a nested component with an onDestroy hook, but it never runs when the component is removed. What is the most likely cause?
medium
A. The parent component's onDestroy prevents child's onDestroy
B. The component is never actually removed from the DOM
C. You used onMount instead of onDestroy
D. You forgot to import onDestroy from 'svelte'

Solution

  1. Step 1: Check if component is removed from DOM

    onDestroy only runs when the component is removed. If it stays in DOM, it won't run.
  2. Step 2: Verify other causes

    Importing onDestroy incorrectly or mixing hooks causes errors, but the main reason is component not unmounting.
  3. Final Answer:

    The component is never actually removed from the DOM -> Option B
  4. Quick Check:

    onDestroy runs only on removal [OK]
Hint: onDestroy runs only when component leaves DOM [OK]
Common Mistakes:
  • Expecting onDestroy without unmounting
  • Confusing onMount with onDestroy
  • Assuming parent's lifecycle affects child's onDestroy
5. You have a nested component that fetches data in onMount and cleans up a subscription in onDestroy. If the parent conditionally renders this child multiple times, what is the best practice to avoid memory leaks?
hard
A. Avoid conditional rendering of the child component
B. Move the data fetching and subscription to the parent component only
C. Use beforeUpdate in the child to reset subscriptions
D. Ensure the child's onDestroy properly unsubscribes every time it unmounts

Solution

  1. Step 1: Understand lifecycle with conditional rendering

    When the child mounts and unmounts repeatedly, onDestroy must clean up resources each time to prevent leaks.
  2. Step 2: Evaluate best practice for cleanup

    Properly unsubscribing in onDestroy ensures no leftover subscriptions remain after unmount.
  3. Final Answer:

    Ensure the child's onDestroy properly unsubscribes every time it unmounts -> Option D
  4. Quick Check:

    Clean up in onDestroy to avoid leaks [OK]
Hint: Always clean subscriptions in onDestroy [OK]
Common Mistakes:
  • Ignoring cleanup in onDestroy
  • Moving logic unnecessarily to parent
  • Using beforeUpdate for cleanup instead of onDestroy
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard