Skip to content

Skip registering event handlers when delegate is null - #67553

Open
Yuvan111 wants to merge 11 commits into
dotnet:mainfrom
Yuvan111:fix-18118-event-null-handle
Open

Yuvan111 wants to merge 11 commits into
dotnet:mainfrom
Yuvan111:fix-18118-event-null-handle

Conversation

@Yuvan111

@Yuvan111 Yuvan111 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Skip registering event handlers when delegate is null

Description

This change prevents Blazor from registering event handlers that require an explicit receiver when no delegate is assigned. Previously, events such as onmousemove could still be included in the render tree when the callback was null, resulting in unnecessary event dispatching and potential re-renders. The condition has been updated from value.RequiresExplicitReceiver to value.RequiresExplicitReceiver && value.HasDelegate, ensuring that only valid event handlers are registered and reducing unnecessary client-server event traffic for optional event callbacks.

Fixes #18118

@Yuvan111
Yuvan111 requested a review from a team as a code owner July 2, 2026 03:37
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 2, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @Yuvan111. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@Youssef1313 Youssef1313 added the area-blazor Includes: Blazor, Razor Components label Sep 2, 2026
Copilot AI lite review requested due to automatic review settings September 23, 2026 06:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Add browser regression coverage for null callbacks not being dispatched or causing re-renders.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

This PR prevents Blazor from registering event handlers when their callbacks have no delegate.

Changes:

  • Adds HasDelegate checks for typed and untyped event callbacks.
  • Adds unit tests verifying null callbacks are omitted.
File Summary
src/​Components/​Components/​test/​Rendering/​RenderTreeBuilderTest.cs Tests omission of event frames for null callbacks.
src/​Components/​Components/​src/​Rendering/​RenderTreeBuilder.cs Skips registering event handlers without delegates.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Sep 30, 2026

@PureWeen PureWeen left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Written with GitHub Copilot (AI), which did the code reading and the local test runs described below ("I" refers to those runs). Posted by PureWeen.

Looked at this again at 20f98aa. Unfortunately I don't think it fixes #18118 yet, and one part of the change regresses; details inline.

I ran the Components tests locally (all green at head), and then wrote small repro tests against both the merge-base and this head:

  • A null delegate bound through EventCallback.Factory.Create(this, ...) still gets a frame and a registered handler at both, and clicking still re-renders. So the scenario in the issue isn't changed.
  • Boxed receiver-only null callbacks through the object / @attributes path were dropped at the merge-base and now register a handler. That's the regression.

No test in the PR fails before and passes after the change, including the E2E one (inline). The PR description also still describes an earlier version of the change.

I didn't run the E2E tests (no browser available on my machine).

@Yuvan111

Yuvan111 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @PureWeen . Addressed all review feedback by restoring HasDelegate-based checks across typed and boxed EventCallback paths, ensuring receiver-only callbacks with null delegates are correctly omitted. Added unit coverage proving the regression and validating the fix across typed, untyped, and boxed callback scenarios.

Updated the E2E test to reproduce the original scenario using a nullable Action bound through Razor instead of default(EventCallback). The test now verifies browser event registration directly, confirming no handlers are registered when the delegate is null and that registration occurs once the delegate is enabled.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor: Component re-renders even when onmousemove is null

4 participants