Effects and Side Effects
React to signal changes with effects.
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"All lessons in this course
- Computed Signals
- Effects and Side Effects
- Effect Cleanup and Lifecycle
- linkedSignal and Advanced Patterns