0
0
Svelteframework~20 mins

onMount in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
onMount Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when onMount runs in this Svelte component?

Consider this Svelte component code:

import { onMount } from 'svelte';

let message = 'Waiting...';

onMount(() => {
  message = 'Component is ready!';
});

<h1>{message}</h1>

What will the user see when this component first appears?

Svelte
import { onMount } from 'svelte';

let message = 'Waiting...';

onMount(() => {
  message = 'Component is ready!';
});

<h1>{message}</h1>
AThe component throws an error because message cannot be changed inside onMount.
BThe heading immediately shows 'Component is ready!' without showing 'Waiting...'.
CThe heading stays 'Waiting...' forever because onMount does not run.
DThe heading first shows 'Waiting...' then changes to 'Component is ready!' after mount.
Attempts:
2 left
💡 Hint

Remember that onMount runs after the component appears in the page.

lifecycle
intermediate
1:30remaining
Which statement about onMount is true?

Choose the correct description of onMount in Svelte.

A<code>onMount</code> runs once after the component is first added to the DOM.
B<code>onMount</code> runs every time the component updates its state.
C<code>onMount</code> runs only when the component is removed from the DOM.
D<code>onMount</code> runs before the component is created.
Attempts:
2 left
💡 Hint

Think about when you want to run code that needs the component visible.

🔧 Debug
advanced
2:30remaining
Why does this onMount code not update the UI?

Look at this Svelte component snippet:

import { onMount } from 'svelte';

let count = 0;

onMount(() => {
  var count = count + 1;
});

<p>Count: {count}</p>

But the displayed count stays 0. Why?

Svelte
import { onMount } from 'svelte';

let count = 0;

onMount(() => {
  var count = count + 1;
});

<p>Count: {count}</p>
ABecause <code>onMount</code> runs too early before the UI is ready to update.
BThe <code>var count</code> shadows the outer reactive <code>let count</code>, so the assignment affects a local variable instead.
CBecause the assignment inside <code>onMount</code> does not trigger reactivity without <code>$:</code>.
DBecause <code>count</code> is a constant and cannot be changed.
Attempts:
2 left
💡 Hint

Check variable declarations and scoping inside the callback function.

📝 Syntax
advanced
1:30remaining
Which onMount usage is syntactically correct in Svelte?

Choose the correct way to use onMount to run code after component mount.

AonMount { console.log('Mounted'); }
BonMount(() =&gt; { console.log('Mounted'); });
ConMount(() =&gt; console.log('Mounted'))
DonMount(() =&gt; { return console.log('Mounted'); });
Attempts:
2 left
💡 Hint

Remember the syntax for calling onMount with an arrow function.

state_output
expert
3:00remaining
What is the final value of status after this Svelte component mounts?

Analyze this code:

import { onMount } from 'svelte';

let status = 'init';

onMount(() => {
  status = 'loading';
  setTimeout(() => {
    status = 'done';
  }, 100);
});

<p>Status: {status}</p>

What will status show 200ms after the component appears?

Svelte
import { onMount } from 'svelte';

let status = 'init';

onMount(() => {
  status = 'loading';
  setTimeout(() => {
    status = 'done';
  }, 100);
});

<p>Status: {status}</p>
A'done'
B'loading'
CAn error occurs because setTimeout is not allowed inside onMount
D'init'
Attempts:
2 left
💡 Hint

Think about asynchronous updates and timing.