I want to put up a Loading… overlay while a page is loading. If everything was in my razor page then I could make it visible as the first line in OnInitializedAsync() and hide it as the last line.
But I have children components in the page. And as they all also have their own OnInitializedAsync(), and that is async for all of them, they complete in a random order.
So, for making the overlay visible, is the containing page’s OnInitializedAsync the first to be called?
And is OnAfterRenderAsyncwhere I should then hide it? Or can I do so in OnInitialized (no Async)? Or somewhere else?
I need this not only for the UI letting the user know the page is loading, but also for my bUnit tests to have it WaitForState() until the page is fully rendered. I can test for the IsLoading property to be false.
Here's a demo to show how the render sequence works and how the LoadingOverlay can work with sub-components.
Take the
LoadingOverlayfrom the other answer:Add the Blazr.BaseComponents package to the project.
DocumentedComponentBaseis used as the base component for the components to document their lifecycle and event sequences.Add
WeatherList.RazorUpdate
FetchData:If you now run this code you will see the following output to the console by
DocumentedComponentBase:Note that
FetchDataruns it's OnInitialized sequence to completion and renders and runs it's firstOnAfterRenderAsyncbeforeWeatherListis even created.The above documented sequence demonstrates that there is no way you can set a flag in
FetchDatathat shows a Loading message that you can guarantee will not complete before sub-components start and run theirOnInitializedsequences.