0
0
Svelteframework~20 mins

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

Choose your learning style9 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.