Menu

Things to Watch Out For When Creating Components That Relay DOM Events in Blazor

DEV Community: blazorΒ·jsakamotoΒ·7 months ago
#phBEUnBf
#dev#class#code#onmouseover#event
Long article (1080 words) β€” no AI summary yet.
Reading 0:00
15s threshold

πŸ”° 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 .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.