adev/src/content/tutorials/signals/steps/2-deriving-state-with-computed-signals/README.md
Now that you've learned how to create and update signals, let's learn about computed signals. Computed signals are derived values that automatically update when their dependencies change. They're perfect for creating reactive calculations based on other signals.
In this activity, you'll learn how to use the computed() function to create derived state that updates automatically when the underlying signals change.
Let's enhance our user status system by adding computed values that derive information from our user status signal. The starter code now includes three status options: 'online', 'away', and 'offline'.
// Add computed to existing imports
import {Component, signal, computed, ChangeDetectionStrategy} from '@angular/core';
notificationsEnabled = computed(() => this.userStatus() === 'online');
This computed signal will automatically recalculate whenever the userStatus signal changes. Notice how we call this.userStatus() inside the computed function to read the signal's value.
</docs-step>
statusMessage = computed(() => {
const status = this.userStatus();
switch (status) {
case 'online':
return 'Available for meetings and messages';
case 'away':
return 'Temporarily away, will respond soon';
case 'offline':
return 'Not available, check back later';
default:
return 'Status unknown';
}
});
This shows how computed signals can handle more complex logic with switch statements and string transformations. </docs-step>
<docs-step title="Create a computed signal that calculates working hours availability"> Add a computed signal that calculates if the user is within their working hours.isWithinWorkingHours = computed(() => {
const now = new Date();
const hour = now.getHours();
const isWeekday = now.getDay() > 0 && now.getDay() < 6;
return isWeekday && hour >= 9 && hour < 17 && this.userStatus() !== 'offline';
});
This demonstrates how computed signals can perform calculations and combine multiple data sources. The value updates automatically when the userStatus changes.
</docs-step>
For notifications, replace Loading... with an @if block:
@if (notificationsEnabled()) {
Enabled
} @else {
Disabled
}
For the message, replace Loading... with:
{{ statusMessage() }}
For working hours, replace Loading... with an @if block:
@if (isWithinWorkingHours()) {
Yes
} @else {
No
}
Notice how computed signals are called just like regular signals - with parentheses! </docs-step>
</docs-workflow>Excellent! You've now learned how to create computed signals.
Here are some key points to remember:
In the next lesson, you'll learn about a different way to derive state with linkedSignals!