Introducing the component lifecycle
Lifecycle events are hooks that allow us to jump into specific stages in the lifecycle of a component and apply custom logic. They are optional to use but might be valuable if you understand how to use them.
Some hooks are considered best practices, while others help debug and understand what happens in an Angular application. A hook has an interface defining a method we need to implement. The Angular framework ensures the hook is called, provided we have implemented this method in the component.
Defining the interface in the component is not obligatory but is considered a good practice. Angular cares only about whether we have implemented the actual method or not.
The most basic lifecycle hooks of an Angular component are:
ngOnInit
: This is called when a component is initializedngOnDestroy
: This is called when a component is destroyedngOnChanges
: This is called when values of input binding properties...