How Lifecycle methods in Blazor Work?

How Lifecycle methods in Blazor Work?

The Methods in the Lifecycle of Blazor App

During their lifecycle, Blazor components undergo the following synchronous as well as asynchronous lifecycle methods

  1. OnInitialized()/OnInitializedAsync() – These are invoked when the component is ready to start, having received its initial parameters for its parents in the render tree. For an asynchronous operation, this method is overridden when we want the component to refresh after completion of the operation.
  2. OnParametersSet()/OnParametersSetAsync() – When the component has received its parameters from its parents in the render tree and the incoming values having been assigned to the properties, this method is invoked.
  3. OnAfterRender()/OnAfterRenderAsync() – These methods are invoked each time after the component has been rendered.

Now let’s proceed with the GetStartedBlazor application in Visual Studio 2019 which we started earlier here with templated components and added the routing here and add a component called LifecycleTest and paste the following code over the existing code:

LifecycleTest.razor

@page "/Lifecycle"


<h3>LifecycleTest</h3>

<p> I was rendered at @TimeRendered</p>
<p> I was rendered at @TimeRenderedAsync</p>



@code {
    private string Title { get; set; }
    private string TitleAsync { get; set; }
    public string TimeRendered { get; set; }
    public string TimeRenderedAsync { get; set; }

    protected override void OnInitialized()
    {
        Title = "Hello World";
        TimeRendered = DateTime.Now.ToString();
    }

    protected override async Task OnInitializedAsync()
    {
        TitleAsync = "Hello World From Async";
        await Task.Delay(2000);
        TimeRenderedAsync = DateTime.Now.ToString();
    }
}

Test the OnInitialized()/OnInitializedAsync() Methods

If we run the application now and browse to the below URL we get the following :

We can see here that the synchronous method was executed two seconds earlier than the asynchronous method and printed the message providing the current time, whereas due to the 2000ms(2 secs) delay in the execution of the asynchronous method, it printed the time of two seconds later.

To test if that both will print the same current time, if we make the delay as 0 like :

 await Task.Delay(0);

Now if we run the application, it will show the same current time:

Now create another Blazor component called LifecycleTest1 and paste the following code to overwrite the default code

LifecycleTest1.razor

@page "/Lifecycle1"
    <h3>OnParametersSet & OnParametersSetAsync Demo</h3>


@foreach (var item in TypeOfEvent)
{
    @item <hr />
}

@code {
    List<string> TypeOfEvent = new List<string>();
    protected override void OnParametersSet()
    {
        TypeOfEvent.Add("OnParameterSet is called");
    }
    protected override async Task OnParametersSetAsync()
    {
        await Task.Delay(2000);
        TypeOfEvent.Add("OnParameterSetAsync is called");
    }

}


Test the OnParametersSet()/OnParametersSetAsync() Methods

If we run the application now and browse to the below URL we get the following :

If we see in realtime, we find that OnParametersSetAsync is called after a two seconds(2000 ms) delay from when the OnParametersSet was called.

YouTube Version

The YouTube version of this blog post is here

Follow Me Here

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.