0
0
Svelteframework~5 mins

onMount in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is onMount in Svelte?

onMount is a function in Svelte that runs code after the component is first added to the page (DOM). It is used to perform setup tasks like fetching data or starting timers.

Click to reveal answer
beginner
When does the onMount function run in a Svelte component?

onMount runs once, right after the component appears on the screen (after it is inserted into the DOM).

Click to reveal answer
beginner
How do you import <code>onMount</code> in a Svelte component?
<p>You import it from <code>svelte</code> like this: <code>import { onMount } from 'svelte';</code></p>
Click to reveal answer
beginner
What kind of tasks is onMount best suited for?

onMount is best for tasks that need to happen once the component is visible, like fetching data, setting up event listeners, or starting animations.

Click to reveal answer
intermediate
How do you clean up resources set up in onMount?

You return a cleanup function inside onMount. This function runs when the component is removed from the page, helping avoid memory leaks.

Click to reveal answer
What does onMount do in Svelte?
ARuns code when the component is destroyed
BRuns code before the component is created
CRuns code after the component is added to the DOM
DRuns code every time the component updates
How do you import onMount in a Svelte file?
Aimport { onMount } from 'svelte';
Bimport onMount from 'svelte/onMount';
Cimport onMount from 'svelte';
Dimport { onMount } from 'svelte/onMount';
Which of these is a good use for onMount?
AFetching data when the component loads
BUpdating the component state on every click
CStyling the component with CSS
DDefining component props
What should you do inside onMount to clean up resources?
ANothing, cleanup is automatic
BReturn a function that cleans up resources
CCall <code>onDestroy</code> manually
DUse <code>setTimeout</code> to delay cleanup
How many times does onMount run during a component's life?
AMultiple times, on every update
BNever, it is optional
CEvery time the component is clicked
DOnce, after the component is added to the DOM
Explain what onMount does in Svelte and give an example of when you would use it.
Think about what happens when a component first appears on the screen.
You got /4 concepts.
    Describe how to clean up resources in onMount and why it is important.
    Consider what happens when a component disappears from the page.
    You got /4 concepts.