Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Return Base Method With Blazor OnInitialized Method

Tags:

c#

blazor

I am seeing more and more examples of OnInitialized and OnInitializedAsync() returning base.OnInitialized[Async]. But Why? The examples on the Microsoft website do not include returning the base method

protected override Task OnInitializedAsync()
{
    Contact = new();
    return base.OnInitializedAsync();
}
like image 984
Barry MSIH Avatar asked May 30 '21 10:05

Barry MSIH


People also ask

What is OnInitialized in Blazor?

OnInitialized and OnInitializedAsync are invoked when the component is initialized after having received its initial parameters in SetParametersAsync. Blazor apps that prerender their content on the server call OnInitializedAsync twice: Once when the component is initially rendered statically as part of the page.

What is Blazor 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.

What is the difference between oninit and oninitasync in Blazor?

The Blazor application provides different synchronous as well as asynchronous lifecycle methods. OnInit & OnInitAsync. The synchronous and asynchronous version of the application methods which gets executed when the component gets Initialized. The OnInitialized is called first, then OnInitializedAsync.

What is the synchronous and asynchronous lifecycle in Blazor?

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. The OnInitialized is called first, then OnInitializedAsync. It is executed when the component is completely loaded.

Why does it cost to add base methods to Razor components?

As @neil-w mentioned, your razor component may inherit another custom component, so in this case if a custom component does something in OnInitialized or in OnInitializedAsync you will broke the logic if you don't call these methods. So, it costs to add base methods calling to avoid possible errors.

Why do we need to add base methods calling?

So, it costs to add base methods calling to avoid possible errors. Also, the right way is to call a creation methods logic in the beginning, and a destruction logic in the end of your function. protected override async Task OnInitializedAsync () { await base.OnInitializedAsync (); Contact = new (); }


Video Answer


1 Answers

It isn't required and you shouldn't add them, just to avoid clutter.

Those life-cycle methods are all virtual empty methods. They are for all intents and purposes abstract but declaring them as such would have required you to override all of them.

Except of course when documented otherwise, as with SetParametersAsync. But there the choice of whether and where you call the base implementation is very much part of your logic, see the "If base.SetParametersAsync isn't invoked" part.

like image 178
Henk Holterman Avatar answered Oct 16 '22 15:10

Henk Holterman