[iOS][Fabric] Recognizer of a live handler stays on a recycled view and takes every drag on the component that reuses it

未关闭
#4,710 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@nihar777 已经在做这个了。

开始于 2026年11月23日。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
objective-c, react-native, typescript
领域
mobile

调研方向

Start in the iOS handler manager where reattachHandlersIfNeeded, maybeBindHandler and flushOperations live, and in gestureRecognizer:shouldReceiveTouch: of the RNGestureHandler recognizer delegate. Run the linked repro (hide the box, show the 60-row ScrollView, drag) on the iOS simulator with New Architecture. Done when the list scrolls and the pan callback count stays at 0 after a recycled RCTViewComponentView is reused.

由索引模型根据 Issue 内容生成。

描述

Platform: iOS
Description

On iOS with the New Architecture, a gesture handler that is bound by view tag (a GestureDetector without the native detector, or a legacy *GestureHandler component) keeps its UIGestureRecognizer attached to the component view after Fabric unmounts that view.

A common trigger is a display: none parent. Fabric then unmounts the subtree and returns the native views to the recycle pool, while the handler stays mounted and registered on the JS side.

The pooled view is later dequeued for an unrelated component. It still carries the recognizer, so the stale handler:

  • receives every touch on the new component and fires its callbacks (onStart and the others), although its own view is hidden;
  • blocks an ancestor ScrollView, whose pan never wins, so the list does not scroll. Taps still work.

RNGestureHandlerManager reattachHandlersIfNeeded is meant to handle this case, but it does not:

  • It returns early from maybeBindHandler when the view for handler.viewTag is nil or has no superview.
  • It never removes the recognizer from handler.recognizer.view, the recycled view.
  • It runs only from flushOperations, so nothing runs when the mount that reuses the view queues no gesture-handler operation.

#4440 fixed the same leak for RNGestureHandlerDetector (the native-detector path). The view-tag path is unchanged in 3.5.0.

#4340 reported a related symptom for buttons and was closed without a repro.

Steps to reproduce

Repro repository: https://github.com/KeithMoc/rngh-recycle-repro

  1. Launch the app on an iOS simulator (New Architecture).
  2. Tap "1. Hide box". The box's parent gets display: none.
  3. Tap "2. Show list". A ScrollView with 60 rows mounts.
  4. Drag the list vertically.

Expected: the list scrolls, and "pan events" stays at 0 because the box with the pan is hidden.

Actual: the list does not scroll, and "pan events" counts up on every drag.

Measured on the iOS 26.1 simulator, RN 0.86.3, RNGH 3.5.0, Debug build:

  • Steps 2 → 4: 3 of 3 runs FROZEN, at 3 drag positions each. "pan events" reached 3 in every run.
  • Control, step 3 only (box stays visible): 3 of 3 runs scroll, with "pan events" at 0.
  • In lldb: the stray RNBetterPanGestureRecognizer sits on RCTViewComponentView tag 524. Its handler's viewTag is 36.
// App.tsx (core of the repro)
const pan = Gesture.Pan().runOnJS(true).onStart(() => setPanEvents(n => n + 1));

<View style={{display: boxHidden ? 'none' : 'flex'}}>
  <GestureDetector gesture={pan}>
    <View style={{height: 120, backgroundColor: '#88c'}} />
  </GestureDetector>
</View>
{listShown && (
  <ScrollView>{ROWS.map(i => <View key={i} style={{height: 56}}><Text>Row {i}</Text></View>)}</ScrollView>
)}
Proposed fix
  1. Ignore touches on a recycled view. RNGestureHandler gestureRecognizer:shouldReceiveTouch: returns NO while the recognizer's view is an RCTViewComponentView whose tag differs from viewTag. Fabric sets view.tag when it dequeues a view, so a mismatch means the view was recycled. This is a single comparison per touch. Measured with only step 1 applied to 3.5.0:
    • Repro: 3 of 3 runs scroll, with "pan events" at 0. The stray recognizer is still attached but no longer receives touches.
    • Visible box: its pan still fires (3 drags, 3 events).
    • Control: 2 of 2 runs scroll.
  2. Detach the recognizer. In reattachHandlersIfNeeded, remove the recognizer from recognizer.view when that view is no longer the view registered for viewTag. Keep viewTag so the handler rebinds when its view returns.
  3. Run the reattach after every mount. Today it runs only from flushOperations; it should also run after each Fabric mount transaction.

We can open a PR with step 1, or with all three steps, if that direction is acceptable.

Gesture Handler version

3.5.0 (also seen on 3.0.2)

React Native version

0.86.3

Platforms

iOS

JavaScript runtime

Hermes

Workflow

React Native (without Expo)

Architecture

New Architecture (Fabric)

Build type

Debug mode

Device

iOS simulator

Device model

iPhone 17 Pro simulator, iOS 26.1

主要语言
TypeScript
星标
6.8k
派生
1.1k
平均合并
2 天 17 小时
30 天内合并 PR
28

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

software-mansion/react-native-gesture-handler 的其他 Issue

查看 software-mansion/react-native-gesture-handler 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。