Lifecycle events
There are a couple of lifecycle events we can use to run our code. In this section, we will go through them and see when we should use them. Most lifecycle events have two versions – synchronous and asynchronous.
OnInitialized and OnInitializedAsync
When the component is fully loaded, OnInitialized()
is called and then OnInitializedAsync()
. This is a great method to load any data as the UI has not been rendered yet at this point. If we are doing any long-running tasks (such as getting data from a database), we should put that code in the OnInitializedAsync()
method.
These methods will not run again if a parameter changes (see OnParameterSet()
and OnParameterSetAsync()
).
OnParametersSet and OnParametersSetAsync
OnParameterSet()
and OnParameterSetAsync()
are called when the component is initialized (after OnInitialized()
and OnInitializedAsync()
), and whenever we change the value of a parameter.
If we, for example, load data in the OnInitialized()
method...