79676338

Date: 2025-06-23 14:43:36
Score: 1.5
Natty:
Report link

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

Reasons:
  • Blacklisted phrase (0.5): Best regards
  • Blacklisted phrase (1): regards
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Marcus