Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 19, 2024 03:20 am GMT

Angular Signals Base untracked function

The computed function is used to derive values from any number of signals.

The Computed signal are read-only signals that derive their value from other signals.

We can define computed signals using the computed function and specifying a derivation. For instance:

firstName = signal('Signals');lastName = signal('Test');fullName = computed(() => this.firstName() + ' ' + this.lastName());

In the above example, changing the value of either firstName or lastName will automatically update the value of fullName.

What if we wanted fullName to update only when fistName changes but not when lastName changes?

The solution to that problem is a function called untracked:

fullNameUntracked = computed(() => this.firstName() + ' ' + untracked(() => this.lastName()));

The untracked function allows us to read the value of a signal without making such signal a dependency of our computed signal or effect.

A complete example is here https://stackblitz.com/edit/stackblitz-starters-yj8qak?file=src%2Fmain.ts

I hope you found it helpful. Thanks for reading.

Let's get connected! You can find me on:


Original Link: https://dev.to/nhannguyendevjs/angular-signals-base-untracked-function-2m8i

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To