Runs a callback function fn that can read signal values without establishing a reactive subscription.
Normally, reading a signal's value (via .value or ()) inside an effect
or a computed callback
automatically subscribes the surrounding context to that signal. If the signal changes, the context is
re-executed.
In some scenarios, you want to read a signal's current value inside a reactive context but avoid creating a subscription. This is where untracked is useful. It temporarily suspends active tracking, executes fn, and then restores tracking.
Parameters:
- fn: The callback function to execute. Any signal read inside this callback will not register a dependency.
Returns:
- The value returned by the callback function fn.
Example Usage#
import 'package:preact_signals/preact_signals.dart';
void main() {
final counter = signal(0);
final loggingThreshold = signal(5);
effect(() {
final currentCount = counter.value; // Establishing a subscription to `counter`
// We want to read `loggingThreshold` but we do NOT want this effect to
// trigger whenever `loggingThreshold` changes.
final threshold = untracked(() => loggingThreshold.value);
if (currentCount > threshold) {
print("Counter ($currentCount) has exceeded the threshold ($threshold)!");
}
});
counter.value = 6; // Prints: "Counter (6) has exceeded the threshold (5)!"
// Updating the threshold will NOT trigger the effect, because it was read inside `untracked`
loggingThreshold.value = 10;
}