OK, so I add here a full explanation of what I am designing.
I have multiple CRUD pages. When a user click on a button, for example SAVE
, I will call API to save it. Meanwhile, I will show the spinner or whatever animation on that SAVE
button. Let's call that LoadingButton
component.
<button class="btn @ButtonCssClass" form="@ForForm" type="@Type" disabled="@IsLoading" @onclick="Click">
@if (IsLoading)
{
<span class="spinner-border spinner-border-sm text-light" aria-hidden="true"></span>
<span role="status">@LoadingText</span>
}
else
{
if (!string.IsNullOrEmpty(IconCss))
{
<i class="@IconCss me-2"></i> @NormalText
}
else
{
@NormalText
}
}
</button>
@code {
[Parameter] public string ButtonCssClass { get; set; } = string.Empty;
[Parameter] public string NormalText { get; set; } = string.Empty;
[Parameter] public string LoadingText { get; set; } = string.Empty;
[Parameter] public string ForForm { get; set; } = string.Empty;
[Parameter] public string Type { get; set; } = "button";
[Parameter] public string IconCss { get; set; } = string.Empty;
[Parameter] public EventCallback<MouseEventArgs> Click { get; set; }
private bool IsLoading { get; set; }
public void StartLoading()
{
IsLoading = true;
StateHasChanged();
}
public void StopLoading()
{
IsLoading = false;
StateHasChanged();
}
}
So on one of my dialog, for example EditProductDialog, I will use that button
<LoadingButton @ref="SubmitButton" IsLoading="_isLoading" Type="submit" IconCss="fa-solid fa-floppy-disk" ForForm="FormProduct" ButtonCssClass="btn-primary rounded-pill me-2" LoadingText="SAVING" NormalText="SAVE" />
and use it in Submit method
private async Task Submit()
{
_isLoading = true;
await Task.Delay(5000);
_isLoading = false;
}
and as the error on the first post, it throws the exception at the Mediator call. If I remove the assignment to the _isLoading, things just work fine.
Another problem which I don't know why
[2024-03-11T13:35:21.071Z] Error: System.AggregateException: One or more errors occurred. (TypeError: Cannot read properties of null (reading 'insertBefore'))
---> System.InvalidOperationException: TypeError: Cannot read properties of null (reading 'insertBefore')
at Microsoft.AspNetCore.Components.RenderTree.Renderer.InvokeRenderCompletedCallsAfterUpdateDisplayTask(Task updateDisplayTask, Int32[] updatedComponents)
--- End of inner exception stack trace ---