[]previous and next page

onMount

Runs fn once after the owner has been created and flushed, untracked, so signal changes never run it again.

Runs fn once after the owner has been created and flushed, untracked, so signal changes never run it again. Its return value, if a function, is the cleanup, which runs when the owner is disposed. Never runs on the server. Like useEffect, it waits for a pending <Suspense> boundary to show its content, and warns NO_OWNER_EFFECT in development outside any owner.

Import#

ts
import { onMount } from "@shadow-js/core";

Signature#

ts
function onMount(fn: () => void | Cleanup): void

Parameters#

Name Description
fn Runs once, untracked. It may return a cleanup function.

Examples#

ts
import { onMount } from "@shadow-js/core";

function SearchBox() {
  let input: HTMLInputElement | undefined;
  onMount(() => input?.focus());
  return <input ref={(el: HTMLInputElement) => (input = el)} />;
}