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 OnAfterRenderAsync
where 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
LoadingOverlay
from the other answer:Add the Blazr.BaseComponents package to the project.
DocumentedComponentBase
is used as the base component for the components to document their lifecycle and event sequences.Add
WeatherList.Razor
Update
FetchData
:If you now run this code you will see the following output to the console by
DocumentedComponentBase
:Note that
FetchData
runs it's OnInitialized sequence to completion and renders and runs it's firstOnAfterRenderAsync
beforeWeatherList
is even created.The above documented sequence demonstrates that there is no way you can set a flag in
FetchData
that shows a Loading message that you can guarantee will not complete before sub-components start and run theirOnInitialized
sequences.