Lifecycle events
We can use a couple of lifecycle events to run our code. In this section, we will go through them and see when we should use them. Most life cycle events have two versions – synchronous and asynchronous.
OnInitialized and OnInitializedAsync
The first time the component is loaded, OnInitialized()
is called, then OnInitializedAsync()
. This is a great method to load any data, as the UI has not yet been rendered. If we are doing long-running tasks (such as getting data from a database), we should put that code in the OnInitializedAsync()
method.
These methods will only run once. If we want to update the UI when a parameter changes, see OnParametersSet()
and OnParametersSetAsync()
.
OnParametersSet and OnParametersSetAsync
OnParametersSet()
and OnParametersSetAsync()
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...