DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

2 1

Angular Signals Base untracked function

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

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());
Enter fullscreen mode Exit fullscreen mode

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()));
Enter fullscreen mode Exit fullscreen mode

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:

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (2)

Collapse
 
jangelodev profile image
João Angelo

Hi Nhan Nguyen,
Your tips are very useful.
Thanks for sharing.

Collapse
 
nhannguyendevjs profile image
Nhan Nguyen

Thanks.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay