The Hello World project template for Blazor includes a Weather Foreacast example (in addition to the Counter increment example).
I played around with this to see whats going on behind the scenes. I cant seem to figure it out.
Basically, if i comment out the line of code that fetched the weather .json data, then i see "Loading..." indefinitely. Makes sense thus far. But when i run it in its original state, i see "Loading..." then quickly followed by the rendering of the data grid. My confusion is that the rendering of the "Loading..." vs the datagrid is in an if-else statement. So this makes me believe that somehow this if-else is evaluated twice (once on load-time, and 2nd time once the data is loaded).
Questions
I'd like to know what is going on behind the scenes here:
- Is the if-else statement evaluated more than once?
- How else can it evaluate null and then render the grid when its asynchronously not null?
RESOLVED
I found my answer here. My suspicion was correct - the page is indeed rendered twice. The key to this is in the lifecycle of a component.
OnInit is called first, then OnInitAsync. Any asynchronous operations, which require the component to re-render once they complete, should be placed in the OnInitAsync method.
(Note that in the FetchData.cshtml the data is being loaded from OnInitAsync() override.)