Live Chat Icon For mobile
Live Chat Icon

What are the application lifecycle methods in Blazor?

Platform: Blazor| Category: Lifecycle

There are around seven lifecycle methods available in Blazor, which provides synchronous as well as asynchronous lifecycle methods.

OnInitialized ()

This is the synchronous method executed when the component is initialized.

OnInitializedAsync()

This is the asynchronous method executed when the component is initialized.

OnParametersSet()

This is the synchronous method when the component has received the parameter from parent component.

OnParametersSetAsync()

This is an asynchronous method when the component has received the parameter from the parent component.

ShouldRender()

This method is used to suppress the refreshing of the UI. If this method returns true, then the UI is refreshed. Otherwise, changes are not sent to the UI. It always does the initial rendering despite its return value.

OnAfterRender(bool firstRender)

This is the synchronous method executed when the rendering of all the references to the component are populated.[JB1] 

OnAfterRenderAsync(bool firstRender)

This is the asynchronous method which is executed when the rendering of all the references to the component are populated.

@page "/lifecycle"
<h1>Life cycle Methods ..</h1>
@foreach (var item in EventType)
{@item <hr />}
@code {

    List<string> EventType = new List<string>();
    protected override void OnInitialized()
    {
        EventType.Add(" 1 OnInit");
    }
    protected override async Task OnInitializedAsync()
    {
        EventType.Add("2 OnInit Async");
        await Task.Delay(1000);
    }
    protected override void OnParametersSet()
    {
        EventType.Add("3 On Parameter set ");
    }
    protected override async Task OnParametersSetAsync()
    {
        EventType.Add(" 4 OnParametersSet Async Started");
        await Task.Delay(1000);
    }
    protected override bool ShouldRender()
    {
        EventType.Add(" 5 Should render called");
        return true;
    }
    protected override void OnAfterRender(bool firstRender)
    {
        EventType.Add(" 6 OnAfterRenderStarted");
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        EventType.Add(" 7 OnAfterRender Async Started");
        await Task.Delay(1000);
    }
}

 Reference link:

https://docs.microsoft.com/en-us/aspnet/core/blazor/lifecycle?view=aspnetcore-3.1

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.