Explain the difference between EventCallback and a standard C# Action or EventHandler, and why EventCallback is preferred for child-to-parent communication.
Last updated
Invoking an
EventCallbackautomatically re-renders the parent that supplied the handler and dispatches on its renderer context. A plainActiondoes neither, so UI can go stale.EventCallbackis a struct, so an unassigned one is a safe no-op instead of a null reference.Use
EventCallback<T>withInvokeAsync, which properly awaits async handlers.
- https://blazor-university.com/components/component-events/
- https://jonhilton.net/blazor-rendering/
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/blazor/performance/rendering?view=aspnetcore-10.0
- https://github.com/dotnet/aspnetcore/issues/31549
- https://github.com/dotnet/aspnetcore/discussions/29318
- https://dev.to/stevsharp/mastering-blazor-component-communication-eventcallback-cascadingparameter-and-ref-explained-eig
- https://medium.com/@norbertmarkiel/understanding-parent-child-communication-and-state-management-in-blazor-webassembly-726d45e81601