Understanding the component lifecycle
A Razor component has a lifecycle just like any other object. There is a set of synchronous and asynchronous lifecycle methods that can be overridden to help developers perform additional operations during component initialization and rendering.
We can review the Razor component lifecycle in Figure 9.6:
Figure 9.6: Razor component lifecycle
In Figure 9.6, we can see that we can add hooks during the initialization and rendering phases. The following methods can be overridden to catch initialization events:
SetParametersAsync
OnInitialized
andOnInitializedAsync
OnParametersSet
andOnParametersSetAsync
SetParametersAsync
and OnInitialized
(Async
) are invoked only in the first render. OnParametersSet
(Async
) is called every time a parameter is changed.
The following methods can be overridden to customize rendering:
ShouldRender
OnAfterRender
andOnAfterRenderAsync
We will...