Re-render on EventCallback<T> usage cannot be avoided in Blazor: make it possible and at the same time remove reference to this #24655
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Duplicate
Resolved as a duplicate of another issue
Status: Resolved
Is your feature request related to a problem? Please describe.
Considering this component:
Sometimes it's useful to store
RenderFragment
s in fields, e.g. for templates:As soon as the
ValueChanged
parameter is used, this breaks:This doesn't compile, as the usage of
ValueChanged
will insertthis
into the generated code, which isn't available in a static initializer. The crux here: I don't even want the implicit render! So while the initialization can be moved to the constructor, this isn't really an improvement, because there will now the unwanted render.Even in regular markup for a component, there is currently no way to use this component without having an implicit render after the invocation of the callback:
as it translates to (code cleaned up for clarity)
It would be good to have a way to generate an
EvenCallback<T>
without the implicit re-render. This is already feasible with:So far so good for manual generation of
BuildRenderTree
. Sadly there's no way using razor syntax to achieve the same:translates to
So the razor compiler wraps the
EventCallback.Factory.Create<int>(prevent_render, (v) => {})
in aEventCallback.Factory.Create<System.Int32>(this, ___)
. This re-introduces thethis
.Describe the solution you'd like
I would like to have a way to get rid of the callback and with that also the reference to this. This could be done e.g. with
translating to
And
Factory.CreateWithoutRender<T>
can be implemented as (sketch):I would actually already be happy with the razor compiler not wrapping my event callback with another
EventCallback.Factory.Create
, but I think preventing implicit rendering would be useful to many users and the implementation of this can enable the other use case too.The text was updated successfully, but these errors were encountered: