Component Lifecycle Methods
Components have numerous lifecycle methods which can be used to know when the component "will" and "did" load, update, and render. These methods can be added to a component to hook into operations at the right time.
Implement one of the following methods within a component class and Stencil will automatically call them in the right order:
connectedCallback()
Called every time the component is connected to the DOM.
When the component is first connected, this method is called before componentWillLoad.
It's important to note that this method can be called more than once, every time, the element is attached or moved in the DOM. For logic that needs to run every time the element is attached or moved in the DOM, it is considered a best practice to use this lifecycle method.
const el = document.createElement('my-cmp');
document.body.appendChild(el);
// connectedCallback() called
// componentWillLoad() called (first time)
el.remove();
// disconnectedCallback()
document.body.appendChild(el);
// connectedCallback() called again, but `componentWillLoad()` is not.
This lifecycle hook follows the same semantics as the one described by the Custom Elements Spec
disconnectedCallback()
Called every time the component is disconnected from the DOM, ie, it can be dispatched more than once, DO not confuse with a "onDestroy" kind of event.
This lifecycle hook follows the same semantics as the one described by the Custom Elements Spec.
componentWillLoad()
Called once just after the component is first connected to the DOM. Since this method is only called once, it's a good place to load data asynchronously and to setup the state without triggering extra re-renders.
A promise can be returned, that can be used to wait for the first render().
componentDidLoad()
Called once just after the component is fully loaded and the first render() occurs.
componentShouldUpdate()
This hook is called when a component's Prop or State property changes and a rerender is about to be requested. This hook receives three arguments: the new value, the old value and the name of the changed state. It should return a boolean to indicate if the component should rerender (true) or not (false).
A couple of things to notice is that this method will not be executed before the initial render, that is, when the component is first attached to the dom, nor when a rerender is already scheduled in the next frame.
Let’s say the following two props of a component change synchronously:
component.somePropA = 42;
component.somePropB = 88;
The componentShouldUpdate will be first called with arguments: 42, undefined and somePropA. If it does return true, the hook will not be called again since the rerender is already scheduled to happen. Instead, if the first hook returned false, then componentShouldUpdate will be called again with 88, undefined and somePropB as arguments, triggered by the component.somePropB = 88 mutation.
Since the execution of this hook might be conditioned, it's not good to rely on it to watch for prop changes, instead use the @Watch decorator for that.
componentWillRender()
Called before every render().
A promise can be returned, that can be used to wait for the upcoming render.