Skip to solution
hardFrontend

What is the effect() function in Angular Signals, and when should you NOT use it?

103 views
01

Understand the problem

Explain signals-based side effects and their limitations.

signalsreactivity
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

An effect() runs side-effects dynamically when one or more signals inside it change. It is designed for logging, synchronizing with custom DOM elements, or analytics. It should not be used to set other signals (which can cause infinite loop dependencies) or perform logic better suited for computed().

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Proper use of an effect to synchronize state to LocalStorage
import { Component, signal, effect } from '@angular/core';

@Component({
  selector: 'app-theme-selector',
  standalone: true,
  template: '<button (click)="toggleTheme()">Toggle Theme: {{ theme() }}</button>'
})
export class ThemeSelectorComponent {
  theme = signal('light');

  constructor() {
    effect(() => {
      localStorage.setItem('user-theme', this.theme());
    });
  }

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 119 of 121 decoded in the Angular track. One more won't hurt.

Back to track