Overview
onDOMReady(fn) subscribes to the component's DOMReadyEvent. The callback runs via a microtask after the template renders into the host — so the DOM nodes exist and can be queried or measured.
Unlike onMount, which runs once after first attach, onDOMReady fires after each render. Return a cleanup from the handler to tear down work from the previous run before the next fire, and on unmount.
Must be called inside a component's outer (setup) function — calling it at module level throws.
onDOMReady vs onMount vs onPaint
|
API
|
When it runs
|
Notes
|
|---|---|---|
onDOMReady(fn)
| After every render (microtask) | DOM is in the host. May return cleanup. Throws outside a component. |
onMount(fn)
| Once after first DOM ready | Preferred for one-shot setup (timers, subscriptions). Sync may return cleanup. |
onPaint(fn)
| After every paint (double rAF) | Runs after the browser has painted. Prefer for layout reads that need post-paint timing. |
onDOMReady — measure after render
onDOMReady fires after the component's DOM is ready. This demo measures a box after attach — something that would fail if run before the template rendered.