This post is a bit old, but I personally believe that data shouldn't be retrieved from the database via OnInitialized / OnInitializedAsync when server pre-rendering is active. Depending on the size of the result, you might end up waiting twice. That's inefficient. I've decided to use OnAfterRender / OnAfterRender instead.
public partial class ReordEntities
{
private IList<IRecord> Records { get; set; } = new List<IRecord>();
override protected OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// Load data in here
Records = await LoadRecords();
// Rerender UI
StateHasChanged();
}
}
}
Don't forget to call StateHasChanged();
, otherwise the components won't be aware of the loaded data. And make sure you call StateHasChanged();
within the if statement, otherwise a render loop will be created.
If loading the data takes 10 seconds, it makes a difference to me whether I wait 10 or 20 seconds. And in general, I think we shouldn't forget that database resources are quite expensive, and if I can avoid pointlessly retrieving the data from the database twice, I'll do it.
Best regards,
Marcus