The solution ended up being really simple and close to what I had thought should be possible:
In the parent component:
<ShoppingList ShoppingList="@appState.ShoppingList"/>
<Totals Total="@appState.Total"/>
@code {
appState.AddToList(new item{
amount = 1,
name = "beef"
});
}
and then in the components themselves you add a parameter:
@code {
[Parameter]
public List<AppState.Item> ShoppingList{ get; set; }
}
I have an AppState class in my project that keeps track of a list. I also have a component that shows the list to the user, as well as another component that shows some other data by making some calculations using the list.
When I add to the list I want both the visual list component and the calculation component to update, but depending on from which component I add an item it updates either only that component or neither (if I add from the parent).
Is there a way to tell a selection of components that they have to re-render? I managed to get it working when I instantiate the list OnInitialized but when it doesn’t re-render when I make changes.
Some psuedo-code of what I was thinking of:
<ShoppingList @bind="appState.ShoppingList"/>
<Totals @bind="appState.ShoppingList"/>
@code {
appState.AddToList(new item{
amount = 1,
name = "beef"
});
}
I had hoped that this would make the components update when the list changed but sadly that is not the case.