Overview
onMount(fn) registers a callback that runs once after the component's DOM is attached and ready. Use it for subscriptions, timers, focus, measuring elements, and any side effect that needs a live DOM node.
If the sync callback returns a function, that cleanup runs automatically on unmount — the same as calling onCleanup(). Prefer the return-cleanup form when setup and teardown are paired; use onCleanup() when teardown is defined separately or when the mount callback is async.
Async onMount is supported. On the client it runs after DOM-ready like sync; on SSR async mounts are awaited before HTML is serialized so the first paint can include fetched data. Async callbacks cannot return a cleanup — register teardown with onCleanup().
onMount vs onCleanup
|
API
|
When it runs
|
Notes
|
|---|---|---|
onMount(fn)
| Once after DOM ready | Sync or async. Sync may return a cleanup; async cannot. |
onMount(() => cleanup)
| Mount + unmount |
Returned function is registered as cleanup — equivalent to a separate onCleanup.
|
onCleanup(fn)
| On unmount | Call multiple times; all run in registration order. Use for async mounts or split teardown. |
Auto-start timer with cleanup
onMount fires once after the component is attached to the DOM. Returning a function from the callback registers it as a cleanup — called automatically when the component unmounts.
onMount + explicit onCleanup
Use onCleanup() when teardown is defined separately from setup — for example when it depends on a value created outside of onMount.