onMount
onMount registers a function that runs once after the initial render for the current component or root.
Import
import { onMount } from "solid-js";Type
function onMount(fn: () => void): void;Parameters
fn
- Type:
() => void - Required: Yes
Non-tracking function executed once on mount.
Return value
onMount does not return a value.
Behavior
- On the client,
onMountruns once after the initial render. It does not run during server rendering. fndoes not track reactive dependencies.- Internally,
onMount(fn)is equivalent tocreateEffect(() => untrack(fn)). - By the time
onMountruns, refs have already been assigned. - Returning a function from
fndoes not register cleanup. UseonCleanupinsideonMountwhen cleanup is needed.
note
"Mounted" in Solid refers to when a component is rendered within the reactive tree, not when it is physically inserted into the DOM.
If you store JSX in a variable before conditionally rendering it, onMount runs when that JSX is evaluated, even if the elements have not yet been added to the visible page.
Similarly, onCleanup runs based on the reactive ownership tree rather than DOM removal.
For cases where you need to detect actual DOM insertion or removal, consider using a ref callback or the Lifecycle primitives from solid-primitives.
Examples
Access a ref after mount
import { onMount } from "solid-js";
function MyComponent() { let ref: HTMLButtonElement;
onMount(() => { ref.disabled = true; });
return <button ref={ref}>Focus me!</button>;}Run one-time browser setup
import { onMount } from "solid-js";
function Example() { onMount(() => { // Browser-only code console.log(window.location.pathname); });
return <div>Mounted</div>;}