Stop Unwanted Reactivity Loops: What untrack Actually Solves
When you build a reactive UI library without a Virtual DOM, auto-tracking is a superpower—until it reads a signal you never meant to subscribe to.
In a fine-grained reactive model, any reactive computation (like an effect, memo, or dynamic binding) automatically records every signal getter it calls. Most of the time, that is exactly what you want: change the signal, re-run the dependent logic.
In real apps, though, you constantly hit cases where you need one value to trigger updates, while another value is just context or a snapshot at the moment the update runs.
The Problem: Accidental Subscriptions
Suppose you have a counter count that drives an action or log, and a userName (or multiplier or filter) that you only want to read when count changes—not whenever userName changes on its own.
If both getters are called inside the same reactive scope, the runtime treats both as dependencies:
const [count, setCount] = state(0);
const [label, setLabel] = state('clicks');
// In an auto-tracking scope, reading both count() and label()
// usually subscribes the reaction to changes in BOTH signals.
Now imagine label updates while typing in an input box. If label() was tracked inside a heavy update or counter side-effect, changing label fires work that only belonged to count. Worse, if a reaction updates a signal that it also reads indirectly, you can end up with noisy re-runs or hard-to-trace loops.
How untrack Fixes It
untrack lets you read a reactive value without registering it as a dependency of the surrounding reactive scope.
import { state, untrack } from 'levelojs';
const [count, setCount] = state(0);
const [prefix, setPrefix] = state('Total');
function getSummary() {
// Re-runs when count() changes,
// reads prefix() without subscribing to prefix changes.
return `${untrack(() => prefix())}: ${count()}`;
}
Here is what changes mentally and practically:
-
count()stays reactive: Whenevercountupdates, anything depending oncountreacts normally. -
prefix()acts as a non-tracking read: Updatingprefixalone will not force a tracked reaction to re-evaluate just becausegetSummaryran earlier. -
Intent becomes explicit in code: Another developer (or future you) sees immediately that
prefixis read as a snapshot rather than a trigger.
Three Everyday Places Where untrack Matters
-
Logging or analytics inside reactive updates: You want to react when
step()changes, and attach the currentsessionId()without re-running the step logic every timesessionIdis touched. - Reading config or current UI state inside an effect: An effect watches one primary trigger signal and reads a secondary signal only for its current value at execution time.
-
Breaking self-triggered or circular update chains: When reading a piece of state inside a reactive block risks causing redundant or cyclic re-evaluations, isolating that read with
untrackkeeps control in your hands.
Why I Added It to LeveloJS
While refining LeveloJS around fine-grained reactivity, adding Show, For, unmounted, and untrack came down to practical ergonomics rather than adding APIs for the sake of a checklist:
-
ShowandForkeep conditional rendering and lists focused on targeted DOM updates. -
unmountedgives a clear place to clean up resources when nodes leave the DOM. -
untrackgives escape-hatch precision when auto-tracking is too eager.
Auto-tracking should make reactive code simpler—not force you to restructure your state just to avoid unwanted re-runs. untrack is a small API, but it solves a very real day-to-day reactivity trap.
LeveloJS v2.3.1 Is Here!
LeveloJS v2.3.1 was released two days ago, bringing these APIs and improvements, including Show, For, unmounted, and untrack.
With these additions, LeveloJS offers a more complete and flexible fine-grained reactivity experience.
The latest version is currently working smoothly for website development, making it ready for building reactive web applications with a lightweight, direct-DOM approach.
Explore LeveloJS on GitHub:
Top comments (0)