The Blazor application provides different synchronous as well as asynchronous lifecycle methods.
The synchronous and asynchronous version of the application methods which gets executed when the component gets Initialized.
OnInitialized
is called first, then OnInitializedAsync
.OnInitializedAsync
method.@page "/"
<h1>OnInit & OnInitAsync Demo</h1>
@foreach (var item in EventType)
{
@item <hr />
}
@functions{
List<string> EventType = new List<string>();
protected override void OnInitialized()
{
EventType.Add("OnInitialized is called");
}
protected override async Task OnInitializedAsync()
{
EventType.Add("OnInitializedAsync is called");
await Task.Delay(1000);
}
}
The synchronous and asynchronous way of setting the parameter when the component receives the parameter from its parent component.
OnParametersSet
and OnParametersSetAsync
methods are called when a component is first initialised.OnParametersSet
and OnParametersSetAsync
are called each time new or updated parameters are receieved from the parent in the render tree.@page "/"
<h1>OnParametersSet & OnParametersSetAsync Demo</h1>
@foreach (var item in EventType)
{
@item <hr />
}
@functions{
List<string> EventType = new List<string>();
protected override void OnParametersSet()
{
EventType.Add("OnParameterSet is called");
}
protected override async Task OnParametersSetAsync()
{
EventType.Add("OnParametersSetAsync is called");
await Task.Delay(1000);
}
}
The synchronous and asynchronous version of the application methods to perform the additional steps like initializing the other components.
OnAfterRender
and OnAfterRenderAsync
methods are called after each render of the component.@page "/"
<h1>OnInit & OnInitAsync Demo</h1>
@foreach (var item in EventType)
{
@item <hr />
}
@functions{
List<string> EventType = new List<string>();
protected override void OnAfterRender()
{
EventType.Add("OnAfterRender is called");
}
protected override async Task OnAfterRenderAsync()
{
EventType.Add("OnAfterRenderAsync is called");
await Task.Delay(1000);
}
}
This method returns a boolean value, if returns true, it means refresh the UI, otherwise changes are not sent to UI.
The ShouldRender
method always does the initial rendering despite its return value.
@page "/"
<h1>ShouldRender Method Demo</h1>
@foreach (var item in EventType){ @item <hr />}
@functions{
List<string> EventType = new List<string>();
protected override bool ShouldRender()
{
EventType.Add("ShouldRender is called");
return true;
}
}
This method notifies the component that its state has changed.
@page "/refresh-ui-manually"
@using System.Threading;
<h1>@Count</h1>
<button onclick=@StartCountdown>Start Countdown</button>
@functions {
private int Count { get; set; } = 10;
void StartCountdown()
{
var timer = new Timer(new TimerCallback(_ =>
{
if (Count <= 0) return;
Count--;
// Note that the following line is necessary because otherwise
// Blazor would not recognize the state change and not refresh the UI
this.StateHasChanged();
}), null, 1000, 1000);
}
}