When developing an Angular application, there will be instances when we need to run code in response to a certain event, such as when a component is initialized, displayed, or removed from the screen. This is when lifecycle hooks come in handy. For example, if a component has an event listener attached to an HTML element, such as a button click or form submission, we can remove that event listener before removing the component from the screen, just as we can fetch data and display it on the screen after the component has been loaded on the screen.
We may override various methods on a component, such as ngOnInit or ngAfterViewInit, to utilize a lifecycle hook. If these methods are accessible on a component, Angular will call them automatically. This is why these hooks are referred to as lifecycle hooks.