π° Introduction
If you're building a general-purpose Razor component in Blazor, you've probably wanted to expose the DOM events happening inside it as the component's own event callbacks. The OnClick event is a classic example.
So, as a first try, we might implement a MyButton component like this:
< button @onclick = "InternalOnClick" > Click me </ button >
@code { [ Parameter ] public EventCallback < MouseEventArgs > OnClick { get ; set ; }
private async Task InternalOnClick ( MouseEventArgs args ) { await OnClick . InvokeAsync ( args ); } }
I'll explain why in a moment, but the code above is actually a bad practice . At first glance, it looks a little wordy but otherwise fine, right? Well, not quite.
π€ What Is the Problem?
The problem becomes serious once we start relaying high-frequency events , like the mouse hover event @onmouseover .β¦