StateHasChanged method not re render the component once the collection removed. We are tried to Re render the component once the collection item is updated by using StateHasChanged but it executes the next statement. After completion of the all lines then only component re renders.
Main goal is : I want to detect the button deletion once its updated in the UI. So, if StateHasChanged method re render the component then UI will be updated.
Code block
@if (Collection.Count > 0)
{
<div>
@for (var i = 0; i< Collection.Count; i++) {
<button @onclick=@(() => OnDeleteClick())>@Collection[i]</button>
}
</div>
}
@code{
private List<string> Collection = new List<string>();
public class DelArgs
{
public string Text { get; set; }
}
protected override void OnInitialized()
{
Collection.Add("Chai");
Collection.Add("Chang");
Collection.Add("Ikura");
Collection.Add("Anna");
}
private async Task OnDeleteClick()
{
Collection.RemoveAt(0);
StateHasChanged();
Console.WriteLine("Item Deleted!!");
}
}`