Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Blazor – should I read in my data in OnInitializedAsync() or OnAfterRenderAsync(true)

When my page loads, generally I am hitting the database to get the data for that page. It’s not a ton of data so not the end of the world if I read it twice, but better to read it once.

The page can render without this data, but it’s going to look very empty/sparse if it’s viewed without the data. Not wrong, no errors, but empty.

So, when is the suggested place to load the data for the page? And if it’s OnInitializedAsync(), should I have a boolean so I load the first time and subsequent times I know I already have it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • OnInitializedAsync is only called once (per component/page). You don’t have to do anything special. This is the ‘normal’ event to load data. Check the FetchData page in the starter templates.

  • OnParametersSetAsync will be called for each parameter change, it is called at least once.

  • OnAfterRenderAsync(true) is also called once but you are then certain an empty page is already rendered. It works but is not a good idea.

See this picture

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading