Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor EventCallbacks naming conventions - Two-way binding

$
0
0

We're currently in the process of defining some naming conventions for a Blazor application. One interesting point is the naming of EventCallbacks and the handlers.

There isn't too much about this topic on the internet, but commonly found websites (like https://blazor-university.com/components/component-events/), the Microsoft examples, as well as ChatGPT, suggest on prefixing the EventCallbacks with "On", something like:

[Parameter]public EventCallback<CultureInfo> OnCultureChanged { get; set; }

This seems nice and also matches other SPAs (like the discussion here What is the right name of event handler? onClick or handleClick?), but what's strange is now, that we can't use the two-way binding anymore; Blazor has the convention of requiring a EventCallback. Like this:

<TestComponent @bind-SelectedKartenMode:get="SelectedKartenMode"                      @bind-SelectedKartenMode:set="SetSelectedKartenModeAsync"></TestComponent>

The convention dictates, that the subcomponent requires exactly the naming like this:

        [Parameter]        public KartenMode? SelectedKartenMode { get; set; }        [Parameter]        public EventCallback<KartenMode> SelectedKartenModeChanged { get; set; }

Microsoft also dictates explicitly, that it is wrong to use event callbacks for two-way binding, so we either can't use the On-prefix or need some awkward workarounds.

It seems strange to have both sides: Microsoft itself uses the On-prefix, but then not respect it in the convention. Are we doing something wrong here or is there a better workaround than allowing both conventions?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>