Sveltejs Academy · Lesson

onMount: Running After Render

Execute code after the component has been added to the DOM.

Lesson 1 of 413 steps

onMount: Running After Render is a free Sveltejs Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.

onMount Purpose

onMount runs after the component is added to the DOM. Ideal for kicking off async work or accessing refs.

Import It

Import from svelte at the top of your script.

<script>
  import { onMount } from "svelte";
</script>

Basic Use

Pass a function that runs once on mount.

onMount(() => {
  console.log("mounted");
});

Async Operations

You can use async/await inside or return a promise.

onMount(async () => {
  data = await fetch("/api").then(r => r.json());
});

Element Access

Combine with bind:this to manipulate the DOM right after mount.

onMount(() => inputEl.focus());

Cleanup Function

Return a function from onMount to clean up when the component unmounts.

onMount(() => {
  const id = setInterval(tick, 1000);
  return () => clearInterval(id);
});

Server vs Browser

onMount does not run during SSR. Use it for browser-only code like canvas or WebGL setup.

Multiple onMounts

You can call onMount multiple times in one component. They run in declaration order.

Subscriptions

Subscribe to external sources here and unsubscribe in the cleanup return.

Avoid Heavy Work

Defer non-critical work or use requestIdleCallback to avoid blocking the first paint.

Difference from beforeUpdate

onMount runs exactly once; beforeUpdate runs before every reactive update.

Quick Check

How do you clean up an interval started in onMount?

Recap

onMount runs once post-mount; return a function to handle cleanup; never runs on the server.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
41
Lessons
163

Frequently Asked Questions

Is the “onMount: Running After Render” lesson free?

Yes — the full text of “onMount: Running After Render” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO. The Sveltejs Academy course includes 4 lessons in total.

What will I learn in “onMount: Running After Render”?

Execute code after the component has been added to the DOM. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “onMount: Running After Render” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. onMount: Running After Render
  2. onDestroy: Cleanup
  3. beforeUpdate and afterUpdate
  4. tick(): Waiting for DOM Updates
← Back to Sveltejs Academy