Here’s the Blazor components lifecycle:

All Page Load Events

@using Microsoft.Extensions.Logging
@implements IDisposable
@inject ILogger<DemoComponent> Logger

Demo Component

@code {

   public DemoComponent()
   {
       // "Logger" is not initialized yet
   }

   public override async Task SetParametersAsync(ParameterView parameters)
   {
       Logger.LogInformation("SetParametersAsync-start");
       await base.SetParametersAsync(parameters);
       Logger.LogInformation("SetParametersAsync-end");
   }

   protected override void OnInitialized()
   {
       Logger.LogInformation("OnInitialized-start");
       base.OnInitialized();
       Logger.LogInformation("OnInitialized-end");
   }

   protected override async Task OnInitializedAsync()
   {
       Logger.LogInformation("OnInitializedAsync-start");
       await base.OnInitializedAsync();
       Logger.LogInformation("OnInitializedAsync-end");
   }

   protected override void OnParametersSet()
   {
       Logger.LogInformation("OnParametersSet-start");
       base.OnParametersSet();
       Logger.LogInformation("OnParametersSet-end");
   }

   protected override async Task OnParametersSetAsync()
   {
       Logger.LogInformation("OnParametersSetAsync-start");
       await base.OnParametersSetAsync();
       Logger.LogInformation("OnParametersSetAsync-end");
   }

   protected override void OnAfterRender(bool firstRender)
   {
       Logger.LogInformation("OnAfterRender({firstRender})-start", firstRender);
       base.OnAfterRender(firstRender);
       Logger.LogInformation("OnAfterRender({firstRender})-end", firstRender);
   }

   protected override async Task OnAfterRenderAsync(bool firstRender)
   {
       Logger.LogInformation("OnAfterRenderAsync({firstRender})-start", firstRender);
       await base.OnAfterRenderAsync(firstRender);
       Logger.LogInformation("OnAfterRenderAsync({firstRender})-end", firstRender);
   }

   public void Dispose()
   {
       Logger.LogInformation("Dispose");
   }
}
SetParametersAsync-start

    OnInitialized-start
    OnInitialized-end

    OnInitializedAsync-start
    OnInitializedAsync-end

    OnParametersSet-start
    OnParametersSet-end

    OnParametersSetAsync-start
    OnParametersSetAsync-end

SetParametersAsync-end

OnAfterRender(True)-start
OnAfterRender(True)-end

OnAfterRenderAsync(True)-start
OnAfterRenderAsync(True)-end

Sources:

https://www.thinktecture.com/en/blazor/blazor-components-lifecycle-is-not-always-straightforward/

https://www.meziantou.net/asp-net-core-blazor-components-lifecycle.htm
Last modified: November 30, 2020

Author

Comments

Write a Reply or Comment