<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="@IncrementCount">Click me</button>
@code {
[Parameter]
public string IncrementAmount
{
set
{
currentCount = Convert.ToInt32(value);
}
}
public int currentCount { get; set; }
[Parameter] public EventCallback<string> OnClick { get; set; }
async Task IncrementCount()
{
if (OnClick.HasDelegate)
{
currentCount=currentCount+1;
await OnClick.InvokeAsync(DateTime.Now.ToString());//<-commenting this line updates currentCount
}
}
}
Why the currentCount is not getting updated when I click the button in the child component ? The currentCount is getting updated when I comment the OnClick.InvokeAsync line. The OnClick.InvokeAsync is sending the value back to parent withou any problem. (Actually I want to send the currentCount to parent, and since the currentCount not updating I'm sending a dummy datetime value back to parent).
I followed this example: Blazor send input text back to parent component