onMount & onCleanup in SolidJS

From the SolidJS cheat sheet ยท Lifecycle ยท verified Jul 2026

onMount & onCleanup

Run code after the component mounts or when it unmounts.

typescript
// Quick Reference
import { onMount, onCleanup } from "solid-js";

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

onCleanup(() => {
  console.log("Component unmounted");
});
๐Ÿ’ก onMount runs once after the component is first rendered to the DOM
โšก onCleanup works in both components and effects โ€” cleans up between re-runs too
๐Ÿ“Œ Since components run only once, you can set up intervals directly in the function body
๐ŸŸข onMount is equivalent to createEffect that runs only on first execution
lifecyclemountcleanup
Back to the full SolidJS cheat sheet