Overview
Lifecycle hooks run at precise points in a component's life. Register them in the outer function (setup) — never inside the template or an effect. See Mental Model.
Order: outer setup → first render → onMount (DOM ready) → reactive updates → onPaint after each paint → unmount → onCleanup. onAllMount waits for this host and every descendant.
Atoms, effects, and framework hooks are disposed automatically on unmount. You only tear down what you created yourself (timers, third-party listeners, sockets).
Choose a lifecycle API
|
API
|
When
|
Prefer when
|
|---|---|---|
onMount
| Once after DOM ready | Timers, focus, fetch-on-mount. Sync may return cleanup. |
onCleanup
| On unmount | Explicit teardown; required for async mounts. |
onPaint
| After every paint | Layout reads / post-paint work. Guard atom writes. |
onError
| Render/setup throw | Report + drive fallback UI via atoms. |
ref / setRef
| DOM handle | Imperative element access after attach. |
onAllMount
| Subtree mounted | Analytics / layout that needs children ready. |
onMount with cleanup
Register once in setup. Returning a function from sync onMount tears down on unmount.
onPaint — measure after paint
Fires after every paint. Compare before atom.set to avoid loops.