Search code examples
state-managementblazor-webassemblyblazor-component

Blazor webassembly, state management. Refresh child components


I am selecting items from the list, and displaying their different content. It is working correctly with this code:

public class ItemState
{
    public Item CurrentItem { get; set; }
    public event Action OnItemSelected;
    public void SetItemValue(Item item)
    {
        CurrentItem = item;
        NotifyStateChanged();
    }

    private void NotifyStateChanged() => OnItemSelected?.Invoke();
}

protected override void OnInitialized()
{
    ItemState.OnItemSelected += StateHasChanged;
}

In the Item component I am using other components and passing as parameter Item itself, like: <ItemHistory ItemModel="@Item"/> But ItemHIstory component is not refreshing, when selecting different items. There is always shown first selected item's history.

Where is the problem?


Solution

  • The answer was to use StateContainer, so properties then are are accessible for all components. https://learn.microsoft.com/en-us/aspnet/core/blazor/state-management?view=aspnetcore-5.0&pivots=webassembly