Life Cycle Methods Of Component In Blazor

Introduction
 
The component in Blazor derived from BlazorComponent class. The BlazorComponent class contains some specific life cycle method. The current version of Blazor (0.3.0) has some limited life cycle methods.
 
Please remember that Blazor is an experimental project and not yet in production. This article is only valid for Blazor 0.3.0.
 
OnInit() & OnInitAsync()
 
These methods are called when the component has been initialized. This method triggered when the component has received its initial parameters from parent. The OnInit method calls first and then executes OnInitAsync method. Any asynchronous operations, that require the component to re-render on initialization, should be written in OnInitAsync method.
 
OnInit
  1. @page "/"  
  2.   
  3. <h1>Hello, world!</h1>  
  4.   
  5. Welcome to your new app.  
  6. <button class="btn btn-primary" onclick="@ButtonClick">Click me</button>  
  7.   
  8. @functions {  
  9.     protected override void OnInit()  
  10.     {  
  11.         Console.WriteLine("OnInit");  
  12.     }  
  13. }  
OnInitAsync
  1. @page "/async"  
  2.   
  3. <h1>Hello, world!</h1>  
  4.   
  5. Welcome to your new app.  
  6. <button class="btn btn-primary" onclick="@ButtonClick">Click me</button>  
  7.   
  8. @functions {  
  9.     protected override async Task  OnInitAsync()  
  10.     {  
  11.         Console.WriteLine("OnInitAsync ");  
  12.     }  
  13. }  
OnParametersSet() & OnParametersSetAsync()
 
These methods are called after the initialization of component. These methods are called each time when new or updated parameters are received from the parent in the render tree. The properties related to parameter are set before this method is called.
  1. protected override void OnParametersSet()  
  2. {  
  3.       
  4. }  
  1. protected override async Task  OnParametersSetAsync()  
  2. {  
  3.       
  4. }  
OnAfterRender & OnAfterRenderAsync
 
These methods are called after the rendering of component. When these methods are callled, all the elements and child comopents are populated. These methods can be used to bind event listener that requires the elements to be rendered in the DOM. They can also be used to initialize the Javascript library.
  1. protected override void OnAfterRender()  
  2. {  
  3.       
  4. }  
  1. protected override async Task OnAfterRenderAsync()  
  2. {  
  3.       
  4. }  
SetParameters
 
This method is triggered before the parameters are set. This method can be used to modify the parameter before rendering the component. If we overide this method, we need to call base.SetParameters method otherwise incoming parameter cannot be assigned to the properties on the class, hence UI cannot be rendered.
  1. public override void SetParameters(ParameterCollection parameters)  
  2. {  
  3.     ....  
  4.     base.SetParameters(parameters);  
  5. }  
Apart from these life cycle methods, Blazor has other two methods: ShouldRender and StateHasChanged. These methods are not part of the life cycle but they are used in the life cycle process.
 
ShouldRender
 
This method returns boolean and based on the return of this method UI can be re-rendered. It will be called after the component initialization so the component is always initially rendered.
  1. protected override bool ShouldRender()  
  2. {  
  3.     Console.WriteLine("ShouldRender");  
  4.     var renderUI = true;  
  5.   
  6.     return renderUI;  
  7. }  
StateHasChanged
 
This method is used to notify the component that its state has changed. This method is called after any life cycle method. It can also be invoked manually to re-render the UI. This method cannot be overridden.
 
Summary

In this article, we learned about life cycle method for Blazor component. Blazor is an experimental project and not yet in production. This article is only valid in Blazor 0.3.0.
 
You can view or download the source code from the GitHub link here.


Similar Articles