← Back to Angular, page 1

angular-0019 · Medium mid

What is effect() in Angular, when should you use it, and what mistakes should you avoid?

effect() runs a function once and again whenever the signals it read change. It is for side effects that leave the signal world, such as logging, storage, or talking to a non-Angular library. It should not be used to copy one signal into another, because computed or linkedSignal do that better.

Full answer

An effect is a function that Angular runs at least once, records which signals it reads, and runs again when any of them change. Effects are created in an injection context, usually a constructor or field initializer, and are destroyed with their owner.

Good uses are side effects that Angular cannot do with bindings: writing to localStorage, sending an analytics event, drawing on a canvas, or updating a third-party widget. Read the signals, do the outside work, done.

Bad use: deriving state. Code like effect(() => this.total.set(this.a() + this.b())) is a mistake. A computed does it without an extra pass. For a value that is derived but can also be changed by the user, use linkedSignal.

Details worth knowing:

  • Effects run asynchronously as part of change detection, not at the moment of set. Several changes in a row run the effect once.
  • Only signals read synchronously during the run are tracked. Reads after an await or in a callback are not tracked.
  • untracked(() => ...) reads a signal without making it a dependency.
  • The callback receives onCleanup, to register work that must run before the next run or when the effect is destroyed, such as clearing a timer.
  • Writing to signals inside an effect is allowed since Angular 19 (the old allowSignalWrites flag is no longer needed), but it often hides a design problem.
  • Effects are stable in Angular 20. For DOM work after rendering, use afterRenderEffect.
typescript
import { Component, effect, inject, signal, untracked } from '@angular/core';
import { Title } from '@angular/platform-browser';

@Component({
  selector: 'app-theme-switch',
  template: `
    <button (click)="toggle()">Theme: {{ theme() }}</button>
    <button (click)="addClick()">Clicks: {{ clicks() }}</button>
  `,
})
export class ThemeSwitchComponent {
  private readonly title = inject(Title);

  readonly theme = signal<'light' | 'dark'>('light');
  readonly clicks = signal(0);

  constructor() {
    effect((onCleanup) => {
      const current = this.theme();
      // Read without making the effect depend on clicks.
      const seen = untracked(this.clicks);
      this.title.setTitle(`Theme: ${current}`);

      const timer = setTimeout(() => console.log('theme', current, 'after', seen, 'clicks'), 500);
      onCleanup(() => clearTimeout(timer));
    });
  }

  toggle(): void {
    this.theme.update((t) => (t === 'light' ? 'dark' : 'light'));
  }

  addClick(): void {
    this.clicks.update((c) => c + 1);
  }
}

Angular 20.3. Setting the page title is a harmless, visible side effect, so it is used here. The log shows the theme and the click count, but only a theme change reruns the effect.

Key points

  • An effect reruns when the signals it read change, and runs at least once.
  • Use effects for side effects (storage, logging, widgets), not for derived state.
  • Only synchronous reads are tracked; use `untracked` to opt out.
  • `onCleanup` removes timers and listeners before the next run.

Common mistakes

  • Copying one signal into another in an effect. Use `computed` or `linkedSignal`.
  • Reading a signal after an `await` and expecting it to be tracked.
  • Creating an effect outside an injection context without passing an `injector`. It throws.

Follow-ups

  • Why does an effect not run immediately after `set`?
  • How would you create an effect inside a method?
  • What is the difference between `effect` and `afterRenderEffect`?

Tips

  • State the rule: effects are for leaving the signal graph, not for staying in it.
  • Show `untracked` and `onCleanup`. They show you have used effects in real code.
  • If asked about RxJS, say effects are not a replacement for streams that need operators like `debounceTime`.

Evaluation rubric

  • Understands tracking and asynchronous scheduling.
  • Gives correct good and bad uses.
  • Mentions untracked and cleanup.