0
0
Angularframework~10 mins

Why lifecycle hooks matter in Angular - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why lifecycle hooks matter
Component Created
ngOnInit Called
Component Rendered
User Interaction or Data Change
ngOnChanges or ngDoCheck Called
Component Updated
Component Destroyed
ngOnDestroy Called
This flow shows how Angular calls lifecycle hooks at key moments: creation, initialization, updates, and destruction of a component.
Execution Sample
Angular
import { Component, OnInit, OnDestroy } from '@angular/core';

@Component({ selector: 'app-demo', standalone: true, template: `<p>{{message}}</p>` })
export class DemoComponent implements OnInit, OnDestroy {
  message = '';
  ngOnInit() { this.message = 'Hello!'; }
  ngOnDestroy() { console.log('Cleanup done'); }
}
This Angular component sets a message when it starts and logs a message when it is destroyed.
Execution Table
StepLifecycle HookActionComponent StateOutput/Effect
1Component CreatedAngular creates component instancemessage = ''No visible output yet
2ngOnInitSets message to 'Hello!'message = 'Hello!'Template shows: Hello!
3User interacts or data changesNo lifecycle hook yetmessage = 'Hello!'No change
4Component destroyedCalls ngOnDestroymessage = 'Hello!'Console logs: Cleanup done
💡 Component lifecycle ends after ngOnDestroy is called when component is removed
Variable Tracker
VariableStartAfter ngOnInitAfter User InteractionAfter ngOnDestroy
message'''Hello!''Hello!''Hello!'
Key Moments - 2 Insights
Why does ngOnInit run after the component is created but before it is shown?
ngOnInit is designed to initialize data before the component appears on screen, as shown in step 2 of the execution_table where message is set before rendering.
What happens if we try to update the message in ngOnDestroy?
ngOnDestroy is for cleanup, not UI updates. The component is about to be removed, so changes like updating message won't affect the UI, as seen in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'message' after ngOnInit runs?
A'Goodbye!'
B'Hello!'
C''
Dundefined
💡 Hint
Check the 'Component State' column at step 2 in the execution_table.
At which step does the component show 'Hello!' on the screen?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Output/Effect' column in the execution_table.
If we remove ngOnDestroy, what changes in the execution flow?
ANo cleanup message in console
BMessage never sets to 'Hello!'
CComponent never created
DComponent never destroyed
💡 Hint
Refer to step 4 in the execution_table where ngOnDestroy logs cleanup.
Concept Snapshot
Angular lifecycle hooks run at key moments:
- ngOnInit: initialize data before showing
- ngOnChanges/ngDoCheck: detect updates
- ngOnDestroy: cleanup before removal
They help manage component behavior smoothly.
Full Transcript
Angular components go through stages: creation, initialization, updates, and destruction. Lifecycle hooks like ngOnInit run after creation to set up data before the component appears. When data changes, hooks like ngOnChanges detect updates. Finally, ngOnDestroy runs before the component is removed to clean up resources. This flow ensures components behave predictably and efficiently.