Introducing the component lifecycle
Life cycle events are hooks that allow us to spy on 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: It is called when a component is initialized
- OnDestroy: It is called when a component is destroyed
- OnChanges: It is called when values of input binding properties...