Look at this very basic component:
<div>
@param1
<button @onclick="@btn_on_click">Cliquez là</button>
</div>
@code
{
[Parameter]
public int param1 { get; set; }
[Parameter]
public Action<int> on_evt_test_fire { get; set; }
void btn_on_click()
{
param1 += 1;
this.on_evt_test_fire(param1);
}
}
And look at this very basic Page:
@message
<button @onclick="@on_btn_click">Click me</button>
<MyComponent param1="1" on_evt_test_fire="@on_evt" />
<MyComponent param1="2" on_evt_test_fire="@on_evt" />
@code
{
private String message = "";
private void on_evt(int param_evt)
{
message = "Button clicked inside component";
StateHasChanged();
}
private async Task on_btn_click()
{
message = "Button clicked in this page";
}
}
I have a problem with StateHasChanged().
My first question is: Why should i call StateHasChanged in on_evt whereas it is not necessary in on_btn_click. The only difference between this 2 methods is the first is called from inside the component. Why should i call StateHasChanged in this specific case ?
When i call StateHasChanged(), the 2 components are reset: They take their initial values...
Thanks for your help