Adding Actions and EventCallback
To communicate changes, we can use EventCallback
, as shown in the Two-way binding section. EventCallback<T>
differs a bit from what we might be used to in .NET. EventCallback<T>
is a class that is specially made for Blazor to be able to have the event callback exposed as a parameter for the component.
In .NET in general, you can add multiple listeners to an event (multi-cast), but with EventCallback<T>
, you will only be able to add one listener (single-cast).
It is worth mentioning that you can, of course, use events the way you are used to from .NET in Blazor as well. However, you probably want to use EventCallback<T>
because there are many upsides to using EventCallback
over traditional .NET events.
.NET events use classes and EventCallback
uses structs. This means that in Blazor, we don't have to perform a null check before calling EventCallback
because a struct cannot be null.
EventCallback
is asynchronous...