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 of valuable help 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 comes with an interface that defines 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 it 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:
- OnInit: This is called when a component is initialized
- OnDestroy: This is called when a component is destroyed
- OnChanges: This is called when...