0Pricing
Angular Academy · Lesson

Effects and Side Effects

React to signal changes with effects.

Effects and Side Effects is a free Angular Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is an Effect

An effect() runs a function whenever any signal it reads changes. Unlike computed, effects are for side effects: logging, syncing to localStorage, calling APIs, manipulating the DOM.

Effects do not return a value.

Creating an effect

An effect runs once immediately, then re-runs every time a tracked signal changes.

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

const count = signal(0);

effect(() => {
  console.log('count is', count());
});
// logs "count is 0" immediately
count.set(5); // logs "count is 5"

Effects need an injection context

By default, effect() must be created in an injection context — typically a constructor or a field initializer of a component, directive, or service.

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

@Component({ selector: 'app-x', template: '' })
export class XComponent {
  user = signal('guest');
  constructor() {
    effect(() => console.log('user:', this.user()));
  }
}

Automatic dependency tracking

Like computed, effects track exactly the signals they read at runtime. Conditional reads are tracked only when reached.

const enabled = signal(true);
const value = signal(10);

effect(() => {
  if (enabled()) {
    console.log(value());
  }
});
// When enabled() is false, value is not a dependency

Syncing signals to localStorage

A classic effect use case: persist state whenever it changes.

const theme = signal('light');

effect(() => {
  localStorage.setItem('theme', theme());
});

theme.set('dark'); // writes "dark" to localStorage

Effects do not return values

If you need a derived value, use computed(). Effects are for actions with no return; their result is the side effect itself.

// WRONG: trying to derive state in an effect
// effect(() => this.total = this.a() + this.b());

// RIGHT: derive with computed
// total = computed(() => this.a() + this.b());

Setting signals inside effects

By default Angular disallows writing to signals inside an effect to prevent loops. If you truly need it, pass { allowSignalWrites: true } — but prefer computed for derivations.

effect(() => {
  const v = source();
  derived.set(v * 2);
}, { allowSignalWrites: true });
// Use sparingly; computed() is usually better

Manual cleanup with EffectRef

effect() returns an EffectRef. Call .destroy() to stop it manually before the owner is destroyed.

import { effect, EffectRef } from '@angular/core';

const ref = effect(() => console.log(value()));
// later
ref.destroy(); // effect no longer runs

Effects clean up automatically

When created in a component, an effect is automatically destroyed when the component is destroyed. You rarely need to call .destroy() yourself.

Effects run after render

Effects are scheduled and run after change detection, batched per microtask. Multiple synchronous signal changes trigger the effect only once.

const a = signal(0);

effect(() => console.log(a()));

a.set(1);
a.set(2);
a.set(3);
// Effect runs once with the final value 3 (batched)

When to use effect vs computed

computed: derive a value from signals (pure).
effect: react to signals by doing something external (impure).

If you find yourself returning a value from an effect, you probably want a computed.

Quick Check

Test your understanding of effects.

Recap: Effects

effect() runs a function whenever its tracked signals change, for side effects only.

  • Runs once immediately, then on every dependency change.
  • Needs an injection context.
  • Auto-destroyed with its owner; returns an EffectRef for manual control.
  • Use computed for derivations, effect for actions.

Next: cleaning up resources inside effects.

Frequently asked questions

Is the “Effects and Side Effects” lesson free?

Yes — the full text of “Effects and Side Effects” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

What will I learn in “Effects and Side Effects”?

React to signal changes with effects. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Effects and Side Effects” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Computed Signals
  2. Effects and Side Effects
  3. Effect Cleanup and Lifecycle
  4. linkedSignal and Advanced Patterns
← Back to Angular Academy