Thuta Learning
Angular
BasicWeb DevelopmentIntermediate

Lifecycle Hooks

AngularLesson 620 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Lifecycle Hooks
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Lifecycle Hooks, in plain language

Lifecycle Hooks are methods Angular provides so you can run code at each stage of a component's creation and destruction.

Problem → Why → Solution

The problem — why this concept matters

Heavy work or state updates inside frequently invoked hooks such as ngAfterViewChecked can cause performance problems or change-detection loops.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Lifecycle hooks define when code runs as Angular creates a component, updates inputs, renders views, and destroys the instance. Keep constructors focused on dependency wiring and perform input-dependent initialization in ngOnInit or input-change handling. Subscriptions, timers, and observers require framework-assisted or explicit teardown at destruction.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of Lifecycle Hooks

Core syntax or pattern

typescript
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';

export class MyComponent implements OnInit, OnDestroy, OnChanges {
  
  // After the component instance is created and its properties are initialized
  constructor() {
    console.log('Constructor called');
  }
  
  // After the component is initialized and its @Input properties are set
  ngOnInit() {
    console.log('Component initialized');
    // API calls can be started here
  }
  
  // Whenever @Input properties change
  ngOnChanges(changes: SimpleChanges) {
    console.log('Input changed:', changes);
  }
  
  // After the view is initialized
  ngAfterViewInit() {
    console.log('View ready');
  }
  
  // Before the component is destroyed
  ngOnDestroy() {
    console.log('Component destroyed');
    // Cleanup: unsubscribe, clear timers
  }
}

Lifecycle hooks define when code runs as Angular creates a component, updates inputs, renders views, and destroys the instance. Keep constructors focused on dependency wiring and perform input-dependent initialization in ngOnInit or input-change handling. Subscriptions, timers, and observers require framework-assisted or explicit teardown at destruction.

Read the code step by step

  1. input and stateimport { Component, OnInit, OnDestroy, OnChanges } fr...

    Lifecycle Hooks are methods Angular provides so you can run code at each stage of a component's creation and destruction.

  2. component and templatengOnChanges(changes: SimpleChanges) {

    Lifecycle hooks define when code runs as Angular creates a component, updates inputs, renders views, and destroys the instance. Keep constructors focused on dependency wiring and perform input-dependent initialization in ngOnInit or input-change handling. Subscriptions, timers, and observers require framework-assisted or explicit teardown at destruction.

  3. rendered Angular UI}

    Create a child whose input changes twice, log hook order, then add an interval and verify cleanup after navigating away.

Execution flow
  1. input and state
  2. component and template
  3. rendered Angular UI

Success

Explain the core concept and Angular data flow of Lifecycle Hooks

Failure

Heavy work or state updates inside frequently invoked hooks such as ngAfterViewChecked can cause performance problems or change-detection loops.

Lifecycle Hooks are methods Angular provides so you can run code at each stage of a component's creation and destruction.

typescript
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';

export class MyComponent implements OnInit, OnDestroy, OnChanges {
  
  // After the component instance is created and its properties are initialized
  constructor() {
    console.log('Constructor called');
  }
  
  // After the component is initialized and its @Input properties are set
  ngOnInit() {
    console.log('Component initialized');
    // API calls can be started here
  }
  
  // Whenever @Input properties change
  ngOnChanges(changes: SimpleChanges) {
    console.log('Input changed:', changes);
  }
  
  // After the view is initialized
  ngAfterViewInit() {
    console.log('View ready');
  }
  
  // Before the component is destroyed
  ngOnDestroy() {
    console.log('Component destroyed');
    // Cleanup: unsubscribe, clear timers
  }
}
You should see
(Console messages will appear at each stage of the component lifecycle)

Real-world usage

How to apply it in a real project

Create a child whose input changes twice, log hook order, then add an interval and verify cleanup after navigating away.

Mini challenge

Build it yourself

Rebuild the Lifecycle Hooks example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';

export class MyComponent implements OnInit, OnDestroy, OnChanges {
  
  // After the component instance is created and its properties are initialized
  constructor() {
    console.log('Constructor called');
  }
  
  // After the component is initialized and its @Input properties are set
  ngOnInit() {
    console.log('Component initialized');
    // API calls can be started here
  }
  
  // Whenever @Input properties change
  ngOnChanges(changes: SimpleChanges) {
    console.log('Input changed:', changes);
  }
  
  // After the view is initialized
  ngAfterViewInit() {
    console.log('View ready');
  }
  
  // Before the component is destroyed
  ngOnDestroy() {
    console.log('Component destroyed');
    // Cleanup: unsubscribe, clear timers
  }
}

Expected: Explain the core concept and Angular data flow of Lifecycle Hooks

Show hint

Create a child whose input changes twice, log hook order, then add an interval and verify cleanup after navigating away.

Quick quiz 1 / 3

What best describes the core idea of Lifecycle Hooks?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • input and state → component and template → rendered Angular UI
  • Lifecycle hooks define when code runs as Angular creates a component, updates inputs, renders views, and destroys the instance. Keep constructors focused on dependency wiring and perform input-dependent initialization in ngOnInit or input-change handling. Subscriptions, timers, and observers require framework-assisted or explicit teardown at destruction.
  • Heavy work or state updates inside frequently invoked hooks such as ngAfterViewChecked can cause performance problems or change-detection loops.
  • Create a child whose input changes twice, log hook order, then add an interval and verify cleanup after navigating away.

Angular — Components — Angular

Easy traps

  • Heavy work or state updates inside frequently invoked hooks such as ngAfterViewChecked can cause performance problems or change-detection loops.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Lifecycle Hooks example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (Console messages will appear at each stage of the component lifecycle)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Lifecycle Hooks | Thuta Learning