0 day streak
javascript / beginner
Snippet

The onMount Lifecycle Hook

The onMount function schedules a callback to run as soon as the component has been rendered to the DOM. This is the ideal place to fetch data or initialize third-party libraries.

snippet.js
javascript
1
2
3
4
5
6
7
import { onMount } from 'svelte';
let status = 'Initializing...';
onMount(() => {
status = 'Component is ready!';
});
svelte
Breakdown
1
import { onMount } from 'svelte';
Imports the onMount lifecycle function from the Svelte core package.
2
onMount(() => { ... });
Defines the code that should execute only after the component is added to the page.