The idea is to create a base component which calls a service to retrieve the total count of X elements and assign it (refreshed) to a local variable that a inner component uses in a RenderFragment section, so every child component could be generic and only takes care of the count variable.
The problem is that the inner component doesn't get the refreshed variable and always shows 0.
Parent's code:
@typeparam T
@Template
@code {
...
[Parameter]
public RenderFragment Template { get; set; }
[Parameter]
public int Total { get; set; }
...
protected override async Task OnInitializedAsync() {
...
await GetTotal();
}
private async Task GetTotal() {
Total = await ODataClient.CallGETCountMethod();
StateHasChanged();
}
}
Child's code:
<CBaseTotal T="VehiculosList" Total="@Total">
<Template>
<h3>TOTAL INNER TEST: @Total</h3>
<TileLayoutItem Class="card card-stats">
<Content>
<span>TOTAL INNER2 TEST: @Total</span>
</Content>
</TileLayoutItem>
</Template>
</CBaseTotal>
@code {
public int Total { get; set; }
}
UPDATE:
Maybe I'm not in the correct way but made some changes adding a handler and now "Total" is updated but the inner component "TileLayoutItem" is not re-rendering with it.
Parent's code:
@typeparam T
@Template
@code {
...
[Parameter]
public RenderFragment Template { get; set; }
[Parameter]
public EventCallback<int> OnTotalChangedHandler { get; set; }
...
protected override async Task OnInitializedAsync() {
...
await GetTotal();
}
private async Task GetTotal() {
int total = await ODataClientInner.CallGETCountMethod(oDataModel);
await OnTotalChangedHandler.InvokeAsync(total);
StateHasChanged();
}
}
Child's code:
<CBaseTotal T="VehiculosList" OnTotalChangedHandler="@OnTotalChangedHandler">
<Template>
<h3>TOTAL INNER TEST: @Total</h3>
<TileLayoutItem Class="card card-stats">
<Content>
<span>TOTAL INNER2 TEST: @Total</span>
</Content>
</TileLayoutItem>
</Template>
</CBaseTotal>
@code {
public int Total { get; set; }
protected void OnTotalChangedHandler(int total) {
Total = total;
StateHasChanged();
}
}