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 use events the way you are used to from .NET in Blazor. However, you probably want to use EventCallback<T>
because there are many upsides to using EventCallback
over traditional .NET events, as follows:
- .NET events use classes, and
EventCallback
uses structs. This means that in Blazor, we don’t have to perform a null check before callingEventCallback
because a struct cannot be null. EventCallback
is asynchronous...