Demo
HTML
TS
CSS
Signal Example 12 - Untracked Signal
This example demonstrates using untracked() to avoid triggering effects on certain signal updates.
🔄 Untracked Signal
Counter 1 - 1
Counter 2 - 1
To review the changes, view the console. Updating counter 1 triggers the effect, but updating counter 2 does not (due to
untracked()).🔄 Deep Dive: Using untracked() with Angular Signals
Signals are powerful for reactivity, but sometimes you want to read a signal’s value without tracking it—so that updates to that signal don’t trigger effects or recomputations. That’s where untracked() comes in.
Use untracked() to read a signal’s value without creating a dependency. This is useful for logging, analytics, or when you want to avoid unnecessary updates.How This Example Works
- Signal Declarations: Two signals are declared:
counter1 = signal(1)andcounter2 = signal(1). - Effect: An
effect() => { console.log(counter1(), untracked(() => counter2())); }logs both counters to the console, butcounter2is read usinguntracked(). - Updating Counters: Clicking the buttons updates each counter independently.
- Reactivity: Updating
counter1triggers the effect and logs both values. Updatingcounter2does not trigger the effect, because it’s read untracked.
Why Use untracked()?
- Performance: Avoid unnecessary recomputations or effects when you only need a value once.
- Fine-Grained Control: Choose exactly which signals should trigger updates and which should not.
- Debugging & Logging: Log or inspect signal values without affecting reactivity.
- Advanced Patterns: Useful in custom hooks, analytics, or when integrating with non-reactive APIs.
- Real-World Use: Use
untracked()when you want to read a signal’s value without making your effect or computed depend on it.
★With
untracked(), you get precise control over your Angular signal dependencies—making your apps faster and more predictable!