4.19 Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) (Week 4 • Angular Core)
1. Core Concept & Internal Working
Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) (Angular Core) covers Execution sequence from `constructor` -> `ngOnChanges` -> `ngOnInit` -> `ngAfterViewInit` -> `ngOnDestroy` (and modern `DestroyRef.onDestroy` / `viewChild()` signal queries). In production software engineering, it is used to Initializing data subscriptions in `ngOnInit`, measuring DOM elements in `ngAfterViewInit`, and cleaning up timers/streams via `DestroyRef` / `ngOnDestroy`. Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Initializing data subscriptions in `ngOnInit`, measuring DOM elements in `ngAfterViewInit`, and cleaning up timers/streams via `DestroyRef` / `ngOnDestroy`.
3. Granular Subtopics Covered
- Execution sequence from `constructor` -> `ngOnChanges` -> `ngOnInit` -> `ngAfterViewInit` -> `ngOnDestroy` (: Core foundation of Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`): Initializing data subscriptions in `ngOnInit`, measuring DOM elements in `ngAfterViewInit`, and cleaning up timers/streams via `DestroyRef` / `ngOnDestroy`.
- Modern `DestroyRef.onDestroy` / `viewChild()` signal queries): Master 'Modern `DestroyRef.onDestroy` / `viewChild()` signal queries)' inside Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) across real-world inputs.
- Production Pitfall Guard (Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`)): Prevent common defect: Performing heavy HTTP calls or DOM queries inside the class `constructor` before `@Input()` bindings are populated!
- Hands-On Verification (Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`)): Trace lifecycle hook order across parent/child components and register cleanup via `DestroyRef`.
- Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) — Interview Readiness: Explain behavior, trade-offs, and edge cases of Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`).
- Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) — Interview Readiness: Explain behavior, trade-offs, and edge cases of Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`).
4. Reference Implementation
private readonly destroyRef = inject(DestroyRef); ngOnInit(): void { const id = setInterval(() => this.tick(), 1000); this.destroyRef.onDestroy(() => clearInterval(id)); }
5. Common Mistakes to Avoid
- Performing heavy HTTP calls or DOM queries inside the class `constructor` before `@Input()` bindings are populated!
- Skipping boundary, null/empty, or error-state verification when implementing Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`).
- Pushing untested Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) changes without clean naming and Git commit context.
6. Hands-On Exercise
Trace lifecycle hook order across parent/child components and register cleanup via `DestroyRef`.
7. Technical Interview Checkpoints
- Q: What problem does Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) solve in Angular Core, and what fails if it is misused?
Key Focus: Initializing data subscriptions in `ngOnInit`, measuring DOM elements in `ngAfterViewInit`, and cleaning up timers/streams via `DestroyRef` / `ngOnDestroy`. Guard against: Performing heavy HTTP calls or DOM queries inside the class `constructor` before `@Input()` bindings are populated! - Q: Explain the internal working of Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) (Execution sequence from `constructor` -> `ngOnChanges` -> `ngOnInit` -> `ngAfterViewInit` -> `ngOnDestroy` (and modern `DestroyRef.onDestroy` / `viewChild()` signal queries)).
Key Focus: Walk step-by-step through Execution sequence from `constructor` -> `ngOnChanges` -> `ngOnInit` -> `ngAfterViewInit` -> `ngOnDestroy` (and modern `DestroyRef.onDestroy` / `viewChild()` signal queries) with a concrete code example. - Q: How did you apply and verify Component Lifecycle Hooks (`ngOnInit`, `ngOnChanges`, `ngAfterViewInit`, `ngOnDestroy`) in your hands-on lab?
Key Focus: Trace lifecycle hook order across parent/child components and register cleanup via `DestroyRef`.