-
Notifications
You must be signed in to change notification settings - Fork 10.3k
How to retain EventHandlerId #5557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've found another way: blazor can only change the delegate for eventBindings dictionary, keeping same EventHandlerId. here https://github.com/uazo/Blazor/tree/retain-eventhandlerid code and samples
|
@uazo I agree it would be great if we could do that. But won't this break cases where the event handler is meant to change? Example:
|
that code doesn't compile... Making it static all is working as expected. Maybe a problem happens for nested events, with multiple I try to code an example. |
there is no way (for now :) to generate nested events. |
Trying with https://github.com/aspnet/Blazor/issues/802#issuecomment-387953426, it works very well. |
I am stuck with this problem as well. Its kind of funny how this is working. Consider the next code and how it behaves where i is an index:
In this approach I get the correct index of the record but all records OnClick event gets fired.
In this approach only one event gets fired but the index is wrong, it turns to be the length of the array. I have spend days trying to do a work around it and I just went out of ideas for it... |
I don't see your code but maybe you have problem like this: https://github.com/aspnet/Blazor/issues/1402 |
This worked as expected and yes that was my issue! Thanks so much! |
Beyond the performance concerns mentioned above this (anti?-) pattern seems to trigger #6385. I have a table (with lambda event handlers) that grows rapidly to a couple hundred elements, and ends up failing to I was able to work around it by creating a new element and passing the value as a parameter removing the need for a lambda. Before:
After:
It's a little verbose, but hopefully it can get anyone hitting this issue moving until the issue is addressed. |
@SteveSandersonMS is #8232 the desired resolution of this? |
No, I'm not aware that there is any resolution for this. In general we'd have to make the compiler able to know whether a given delegate can change or not, and if it can't, implement some caching logic. It's outside the scope of anything we plan to do in the short term. One useful benefit of moving to newer versions of C# is that the compiler does get smarter about it. For event handlers that are methodgroups (e.g., Since I don't think there's any action for us to take, I'm closing this. CC @rynowak in case he has any more knowledge about where the C# compiler is going with this, or in case he has any new ideas about what we might do inside the Razor compiler to improve it. Ryan, if you do think we could do anything here (even in the long term), please feel free to reopen. |
you must excuse me for the late reply.
Maybe I don't understand but we don't have to check if the delegate is the same or not, my code always replaces it! Invocation:
Assignment:
Why we have to Delete and Recreate the id, after all in the events if something changes is only c# side, in javascript the reference is always to the same event, "onclick" for example (and necessarily because AppendDiffEntriesForAttributeFrame takes for granted that oldTree and newTree refer to the same attribute of the same element).
what am I missing here? |
It's not safe to replace change which delegate an event handler ID refers to. If we did that, then it's possible that:
Keeping the event-handler-ID-to-delegate mapping preserves the association between a user action and whatever data is captured within the delegate's lambda. |
Consider this:
blazor will generate three
EventHandlerId
.When I press the button, the first
EventHandlerId
is retained (just because the ladba "point" at the same address) but the others are simple new labda expression, so blazor will delete and recreate the handler. In a list with many elements and many handler, it's a performance problem.a way to retain can be:
other ideas?
The text was updated successfully, but these errors were encountered: