← Learning Hub
Intermediate

computed(), effect() and linkedSignal()

Signals become powerful when values depend on each other. computed() works out a value from other signals and keeps it up to date. It only recalculates when something it reads has changed.

Think of signals as cells in a spreadsheet. A normal signal is a cell you type into; a computed is a formula cell that recalculates when its inputs change; an effect is like a macro that runs whenever certain cells change and does something outside the sheet (saving, logging). Keeping derived values in computed signals means they can never get out of sync.

Example
import { Component, signal, computed } from '@angular/core';

interface Item { name: string; price: number; qty: number; }

@Component({
  selector: 'app-cart-summary',
  template: `
    @for (item of items(); track item.name) {
      <p>{{ item.name }} x {{ item.qty }}
        <button (click)="inc(item.name)">+</button></p>
    }
    <p>Subtotal: ₹{{ subtotal() }}</p>
    <p>Delivery: ₹{{ delivery() }}</p>
    <h3>Total: ₹{{ total() }}</h3>
  `,
})
export class CartSummary {
  items = signal<Item[]>([
    { name: 'Shirt', price: 799, qty: 1 },
    { name: 'Socks', price: 199, qty: 2 },
  ]);
  subtotal = computed(() => this.items().reduce((s, i) => s + i.price * i.qty, 0));
  delivery = computed(() => (this.subtotal() >= 999 ? 0 : 49));
  total = computed(() => this.subtotal() + this.delivery());

  inc(name: string) {
    this.items.update(list => list.map(i => i.name === name ? { ...i, qty: i.qty + 1 } : i));
  }
}

effect(): run code when signals change

effect() runs a function now, and again every time a signal it reads changes. Use it for side jobs that are not on screen: saving to localStorage, logging, or talking to a non-Angular library.

Example
import { Component, signal, effect } from '@angular/core';

@Component({ selector: 'app-settings', template: `<button (click)="toggle()">Theme: {{ theme() }}</button>` })
export class Settings {
  theme = signal(localStorage.getItem('theme') ?? 'light');

  constructor() {
    effect(() => {
      localStorage.setItem('theme', this.theme());          // runs whenever theme changes
      document.documentElement.dataset['theme'] = this.theme();
    });
  }
  toggle() { this.theme.update(t => (t === 'light' ? 'dark' : 'light')); }
}

linkedSignal(): a value that resets

linkedSignal() is a writable signal that resets itself when another signal changes. Example: the chosen size goes back to the first size whenever the product changes.

Example
product = signal({ name: 'Kurta', sizes: ['S', 'M', 'L'] });
size = linkedSignal(() => this.product().sizes[0]);   // you can still size.set('L')
Don't use effect() to copy one signal into another - use computed() or linkedSignal(). Effects are for the world outside your signals.
Key points
  • computed() derives values that update automatically.
  • effect() runs side work (saving, logging) when signals change.
  • linkedSignal() is writable but resets when its source changes.

📝 Test yourself

1. When does a computed() signal recalculate?
2. What is effect() best used for?
3. Fill in the function that derives a value from other signals:
total = (() => this.a() + this.b());