Sub topic 14
Signals and lifecycle hooks together
This chapter assumes you know the basic hooks (ngOnChanges, ngOnInit, ngOnDestroy) and have used signal() and computed() at a basic level. It shows where signal-based code fits next to those hooks.
Signal inputs replace most ngOnChanges code
A signal input is declared with input() or input.required(). It is a read-only signal, so you call it like a function. Angular sets inputs before ngOnInit, as with decorator inputs. Reading a required input in the constructor fails, because no value exists yet.
The old pattern was to copy a changed input into another field inside ngOnChanges. With signals, you describe the derived value once with computed(). Angular recalculates it only when the input changes.
For side effects such as logging, fetching or starting a timer, use effect(). An effect runs once after creation and again whenever a signal it read changes.
import { Component, computed, effect, input } from '@angular/core';
@Component({
selector: 'app-user-card',
standalone: true,
template: `<p>{{ label() }}</p>`,
})
export class UserCardComponent {
userId = input.required<string>();
label = computed(() => `User: ${this.userId()}`);
constructor() {
effect((onCleanup) => {
const id = this.userId();
console.log('effect runs for', id);
const timer = setTimeout(() => console.log('still showing', id), 1000);
onCleanup(() => clearTimeout(timer));
});
}
}The effect is created in the constructor because effect() needs an injection context. A constructor or field initializer provides one. The effect still sees input values, because Angular runs it during change detection, after inputs are set.
Cleanup is built in
A component's effects are destroyed with the component, so you do not call anything in ngOnDestroy for them. The onCleanup callback runs before the effect re-runs and when it is destroyed. Use it for the timer, listener or request that the previous run started.
Choosing between hooks and signal tools
| Need | Hook approach | Signal approach |
|---|---|---|
| Derive a value from an input | `ngOnChanges` | `computed()` |
| Run a side effect when an input changes | `ngOnChanges` | `effect()` |
| Release resources on destroy | `ngOnDestroy` | `onCleanup`, `DestroyRef` |
| Touch the DOM after rendering | `ngAfterViewInit` | `afterNextRender` |
| Run something once at startup | `ngOnInit` | still `ngOnInit` |
Hooks have not gone away. ngOnInit remains the place for one-time setup that is not tied to a signal. ngDoCheck and the content and view hooks still apply to their own cases.
Gotchas
- Do not write to signals inside an effect unless you have no alternative. Prefer
computed()for derived state, because writes in effects can cause extra runs and hard-to-follow update chains. - An effect reads only the signals it calls during a run. A signal read inside a callback that runs later, such as the
setTimeoutabove, is not tracked. - Effects are not for DOM measurement. Use
afterNextRenderorafterRenderfor that.
Exercise
A component has query = input.required<string>(). You want to call a search API each time query changes and cancel the previous request. Which signal tool do you use, and where does the cancellation go?
Show answer
Use effect() created in the constructor. Read this.query() inside it, start the request with an AbortController, and call controller.abort() inside onCleanup. Angular runs onCleanup before each re-run and when the component is destroyed.