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();
}
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.
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.
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.
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.
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.
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 (); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With