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?
import { onMount } from 'svelte'; let message = 'Waiting...'; onMount(() => { message = 'Component is ready!'; }); <h1>{message}</h1>
Remember that onMount runs after the component appears in the page.
The message variable starts as 'Waiting...'. When the component mounts, onMount runs and updates message to 'Component is ready!'. This triggers Svelte to update the displayed heading. So the user first sees 'Waiting...' then it changes.
onMount is true?Choose the correct description of onMount in Svelte.
Think about when you want to run code that needs the component visible.
onMount runs exactly once after the component is inserted into the page. It does not run on updates or before creation. Cleanup code runs on unmount, but onMount itself runs only once after mount.
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?
import { onMount } from 'svelte'; let count = 0; onMount(() => { var count = count + 1; }); <p>Count: {count}</p>
Check variable declarations and scoping inside the callback function.
The var count = count + 1; inside the onMount callback declares a new local variable named count (using var), which shadows the outer reactive let count. The right-hand side reads the outer count (0), assigns 1 to the local count, but the outer reactive variable remains 0. No reactivity is triggered for the outer variable. To fix, use count = count + 1; without var.
onMount usage is syntactically correct in Svelte?Choose the correct way to use onMount to run code after component mount.
Remember the syntax for calling onMount with an arrow function.
onMount must be called with a function argument using parentheses: onMount(fn). Option A is invalid syntax (missing ()). Options B, C, and D are valid JavaScript, but B matches the standard style in Svelte docs with a block body {}. C is a concise arrow function (valid), D works but the return is unnecessary since console.log returns undefined.
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?
import { onMount } from 'svelte'; let status = 'init'; onMount(() => { status = 'loading'; setTimeout(() => { status = 'done'; }, 100); }); <p>Status: {status}</p>
Think about asynchronous updates and timing.
When the component mounts, status changes to 'loading'. After 100ms, the setTimeout callback runs and sets status to 'done'. After 200ms, the status is 'done'. No errors occur because setTimeout is allowed inside onMount.