I want to load data from an API and display a loading animation.
When I load in OnInitializedAsync() the animation is shown but the API call is made twice.
public class MyComponent : ComponentBase
{
private bool isLoading;
private List<MyData> myData;
protected override async Task OnInitializedAsync()
{
isLoading = true;
myData = await _myService.GetDataAsync();
isLoading = false;
}
}
When I load in OnAfterRenderAsync() the API call is made once but the UI is not updated.
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
isLoading = true;
myData = await _myService.GetDataAsync();
isLoading = false;
StateHasChanged();
}
}
How can I load the data only once and show the loading animation/update the UI properly?