The callback methods work, but are still fairly limited, the approach I took was to use "Mesaging Center" that has been cloned from Xamarin.Forms...
https://github.com/aksoftware98/blazor-utilities
https://www.nuget.org/packages/AKSoftware.Blazor.Utilities/
https://youtu.be/HdEJ4GD9hwM
It's very simple, flexible, and behaves much more like a pubsub messaging system across components interanally which has many benefits.
As a result you can "broadcast" events to individual or multiple components regardless of nesting state and location - super flexible and useful. Taken from their docs...
Place a using in your _imports.razor
@using AKSoftware.Blazor.Utilities
Then put a subscriber in the MainLayout.razor
public void SubscribeToMessage()
{
MessagingCenter.Subscribe<Component1, string>(this, "greeting_message",
(sender, value) =>
{
// Do actions against the value
// If the value is updating the component make sure to call
string greeting = $"Welcome {value}";
StateHasChanged(); // To update the state of the component
});
}
And a publisher in any of the componets you want
public void SendMessage()
{
string valueToSend = "Hi from Component 1";
MessagingCenter.Send(this, "greeting_message", valueToSend);
}
You can put subscribers and senders in any combination of components. I've been making a project where realtime date was updating multiple componenets at the same time, this was a great solution that allowed me to put all my hub connetion logic in MainLayout.razor, and then call and respond to that across all compoents as needed, prvented multiple hub connections being created, and works well in tandem with cascading states.
@onclick=btn_clicked
– Blocky