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?
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