Overview
on(callback, deps) subscribes to each dep via __subscribe and runs callback (scheduled via the effect scheduler) whenever any listed dep notifies. If callback returns a function, that cleanup runs before the next invocation and on final teardown.
Unlike effect, on does not run on create — only when a dependency fires. Dependencies are explicit; reads inside the callback are not auto-tracked for scheduling.
watch is a backward-compatible alias for on. Call on in the component outer function so unmount unsubscribes automatically. The return value is a cleanup function you can also call manually.
on vs effect
|
API
|
Deps
|
Prefer when
|
|---|---|---|
on(fn, deps)
| Explicit array | You want a stable dep list; no run on mount; component-scoped. |
effect(fn)
| Auto-tracked | Immediate first run; deps discovered from reads; OK at module level. |
Explicit deps with on()
Log stays empty until the first count.set() — on() does not run on create.
Cleanup when deps change
Return clearInterval from on() — cleanup runs before the next callback and on unmount. Click Resume to fire the first notify.