Skip to content

Process responder events for touches whose target unmounted - #59522

Closed
lazerg wants to merge 2 commits into
react:mainfrom
lazerg:fix/issue-59519-responder-null-target
Closed

lazerg wants to merge 2 commits into
react:mainfrom
lazerg:fix/issue-59519-responder-null-target

Conversation

@lazerg

@lazerg lazerg commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Summary:

With enableNativeEventTargetEventDispatching on, a touch that ends after its view unmounted never reaches the responder, so the old responder sticks around and trackedTouchCount stays one too high, which drops the next tap. processResponderEvent already handles a null target, but dispatchNativeEvent only accepts a non-null one and would crash in dispatchTrustedEvent if it got null. It now takes EventTarget | null and returns right after the responder step when there is no target. The renderer's dispatchEvent (ReactFabricEventEmitter.js in react/react) still skips the call for a null target, so that guard has to go too before the bug is fully fixed, and this change needs to land first.

Fixes #59519

Changelog:

[GENERAL] [FIXED] - Fix the responder not being released when a touch ends after its view unmounted

Test Plan:

Added a Fantom test to ResponderEventTarget-itest.js that grants the responder, sends topTouchEnd with a null target through dispatchNativeEvent, and checks that the release fires and the next touch is granted again. I couldn't build the Fantom runner locally, so I also ran the null-target case as a throwaway Jest test: on main it throws TypeError: Cannot read properties of null from dispatchTrustedEvent, and it passes with this change. flow check, eslint and prettier are clean.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 27, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 27, 2026
@github-actions

Copy link
Copy Markdown

Visual regression

1 visual difference(s) at 2d382197e434. Visual differences need maintainer approval. Review the report, then apply visual-change-approved.

ios scroll-view/horizontal-scroll-to/end: Changed, 69504 px (18.123%)

diff

Expected and Captured
Expected Captured
expected captured

@meta-codesync

meta-codesync Bot commented Oct 27, 2026

Copy link
Copy Markdown

@rubennorte has imported this pull request. If you are a Meta employee, you can view this in D127796330.

expect(onResponderRelease).toHaveBeenCalledTimes(1);
});

(ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching()

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.

If the behavior should be the same as before, this test should run for both versions of the feature flag and behave the same way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I left this as is for now because the flag-on run can't pass in this PR alone. With the flag off, a touch end with a null target already goes through the legacy responder plugin and releases the responder. With the flag on, ReactFabric's dispatchEvent drops events with a null target, so they never reach dispatchNativeEvent (react/react#38006). After that guard is removed, the test can run for both flag values with the same expectations. Should I keep it here so it lands with the renderer sync, or add it in a follow-up?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I flipped the gate in 6208d24, so the test now runs with the flag off and is skipped with the flag on. With the flag on, the synced renderer still drops touch events whose target is null before they reach dispatchNativeEvent, so that run can't pass until the renderer sync lands (react/react#38006). I left a TODO on the skip to turn it on after the sync.

expect(onResponderGrant).toHaveBeenCalledTimes(1);

// The target is null when the touched view unmounted during the touch.
dispatchNativeEvent(null, 'topTouchEnd', {

@rubennorte rubennorte Oct 27, 2026 •

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.

This is an internal API and we should avoid using them in Fantom tests that should be more e2e. We should find a way to test and verify this using externally visible behaviors (e.g.: dispatching the event while unmounting the component it in the test).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll remove the dispatchNativeEvent import. This case doesn't need a Fantom API change: I can queue the touch end with Fantom.enqueueNativeEvent, unmount the view, and then flush the queue. The native side then sends the event with a null target, the same as on a device. The flag-on run has the blocker from the other thread, so I'll push this test after we agree on how to handle it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 6208d24. The test now uses only Fantom APIs. It queues the touch end, unmounts the view before the event is flushed, and then checks that a touch on a new view gets the responder.

*/
export default function dispatchNativeEvent(
target: EventTarget,
target: EventTarget | null,

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.

I'd use target: ?EventTarget in case we slip an undefined here from React instead of null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to ?EventTarget in 6208d24, also in processResponderEvent. The existing == null check already treats undefined like null.

@rubennorte

Copy link
Copy Markdown
Member

Good catch! Thanks for finding this and submitting a potential fix. Could you please address the comments so we can merge it?

@meta-codesync meta-codesync Bot closed this in f754dbd Oct 27, 2026
@meta-codesync meta-codesync Bot added the Merged This PR has been merged. label Oct 27, 2026
@meta-codesync

meta-codesync Bot commented Oct 27, 2026

Copy link
Copy Markdown

@rubennorte merged this pull request in f754dbd.

@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @lazerg in f754dbd

When will my fix make it into a release? | How to file a pick request?

rubennorte pushed a commit to react/react that referenced this pull request Oct 27, 2026
## Summary

With `enableNativeEventTargetEventDispatching` on, the Fabric renderer
skipped `dispatchNativeEvent` when the touched view had already
unmounted, so the touch end never reached the JS responder and the next
tap got lost. This passes the null target through instead, so React
Native can still release the responder. It needs
react/react-native#59522 to land first, since `dispatchNativeEvent`
there throws on a null target today.

Fixes #38006

## How did you test this change?

Added a test in `ReactFabric-test.internal.js` that unmounts a view,
dispatches a touch end to its old instance handle, and expects
`dispatchNativeEvent` to get a null target. It fails on main and passes
with this change. Also ran `yarn test packages/react-native-renderer`
(default, `--prod`, www-modern), `yarn flow fabric`, `yarn linc` and
`yarn prettier-check`.
github-actions Bot pushed a commit to react/react that referenced this pull request Oct 27, 2026
## Summary

With `enableNativeEventTargetEventDispatching` on, the Fabric renderer
skipped `dispatchNativeEvent` when the touched view had already
unmounted, so the touch end never reached the JS responder and the next
tap got lost. This passes the null target through instead, so React
Native can still release the responder. It needs
react/react-native#59522 to land first, since `dispatchNativeEvent`
there throws on a null target today.

Fixes #38006

## How did you test this change?

Added a test in `ReactFabric-test.internal.js` that unmounts a view,
dispatches a touch end to its old instance handle, and expects
`dispatchNativeEvent` to get a null target. It fails on main and passes
with this change. Also ran `yarn test packages/react-native-renderer`
(default, `--prod`, www-modern), `yarn flow fabric`, `yarn linc` and
`yarn prettier-check`.

DiffTrain build for [dc9aed3](dc9aed3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Taps are lost after a view unmounts during a touch (enableNativeEventTargetEventDispatching)

3 participants