Repository navigation
Simplify VirtualDetector/InterceptingGestureDetector and reduce the number of renders - #3813
Merged
Merged
Conversation
j-piasecki
force-pushed
the
@jpiasecki/optimize-virtual-detector
branch
from
November 13, 2025 09:37
349e799 to
297e0cd
Compare
j-piasecki
force-pushed
the
@jpiasecki/memoize-event-handlers
branch
from
November 13, 2025 09:40
797299c to
86423e1
Compare
j-piasecki
force-pushed
the
@jpiasecki/optimize-virtual-detector
branch
from
November 13, 2025 10:20
297e0cd to
19ea1a9
Compare
j-piasecki
marked this pull request as ready for review
November 13, 2025 11:59
j-piasecki
added a commit
that referenced
this pull request
Nov 13, 2025
…#3814) ## Description Adds an early return inside `removeContextMenuListeners` so that it doesn't throw when `detach` and `dropHandler` are called consecutively. This is possible in v3 api, where the gesture lifecycle isn't tied to the detector component. ## Test plan Found when working on #3813, not sure how to reproduce it outside of it, but to reproduce it there use this: ``` import * as React from 'react'; import { Button, StyleSheet, Text, View } from 'react-native'; import { GestureDetector, InterceptingGestureDetector, useTap, } from 'react-native-gesture-handler'; export const COLORS = { NAVY: '#001A72', KINDA_RED: '#FFB2AD', YELLOW: '#FFF096', KINDA_GREEN: '#C4E7DB', KINDA_BLUE: '#A0D5EF', }; function TextWithTap() { const tap = useTap({ onStart: () => { 'worklet'; console.log('Tapped on text in its own component!'); }, }); return ( <GestureDetector gesture={tap}> <Text style={{ fontSize: 24, color: COLORS.KINDA_GREEN }}> This text is rendered in a separate component. </Text> </GestureDetector> ); } function NativeDetectorExample() { const [entireVisible, setEntireVisible] = React.useState(true); const [firstVisible, setFirstVisible] = React.useState(true); const [secondVisible, setSecondVisible] = React.useState(true); const [thirdVisible, setThirdVisible] = React.useState(true); const [secondKey, setSecondKey] = React.useState(0); const [firstHasCallback, setFirstHasCallback] = React.useState(true); const tapAll = useTap({ onStart: () => { 'worklet'; console.log('Tapped on text!'); }, }); const tapFirstPart = useTap({ onStart: firstHasCallback ? () => { 'worklet'; console.log('Tapped on first part!'); }: () => { 'worklet'; console.log('First part tapped, but no callback set.'); }, }); const tapSecondPart = useTap({ onStart: () => { 'worklet'; console.log('Tapped on second part!'); }, }); return ( <View style={styles.subcontainer}> <Button title={(firstVisible ? 'Hide' : 'Show') + ' entire text'} onPress={() => setEntireVisible(v => !v)} /> <Button title={(firstVisible ? 'Hide' : 'Show') + ' first part'} onPress={() => setFirstVisible(v => !v)} /> <Button title={(secondVisible ? 'Hide' : 'Show') + ' second part'} onPress={() => setSecondVisible(v => !v)} /> <Button title={(thirdVisible ? 'Hide' : 'Show') + ' third part'} onPress={() => setThirdVisible(v => !v)} /> <Button title="Re-mount second part" onPress={() => setSecondKey(k => k + 1)} /> <Button title={(firstHasCallback ? 'Disable' : 'Enable') + ' callback on first text'} onPress={() => setFirstHasCallback(v => !v)} /> {entireVisible && <InterceptingGestureDetector gesture={tapAll}> <Text style={{ fontSize: 18, textAlign: 'center' }}> Some text example running with RNGH {firstVisible && <GestureDetector gesture={tapFirstPart}> <Text style={{ fontSize: 24, color: COLORS.NAVY }}> {' '} try tapping on this part </Text> </GestureDetector>} {secondVisible && <GestureDetector gesture={tapSecondPart}> <Text key={secondKey} style={{ fontSize: 28, color: COLORS.KINDA_BLUE }}> {' '} or on this part </Text> </GestureDetector>} {thirdVisible && <> {' '} <TextWithTap /> </>} {' '} this part is not special :( </Text> </InterceptingGestureDetector>} </View> ); } export default function NativeTextExample() { return ( <View style={styles.container}> <NativeDetectorExample /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, }, subcontainer: { flex: 1, gap: 8, alignItems: 'center', justifyContent: 'center', }, header: { fontSize: 18, textAlign: 'center', paddingHorizontal: 24, }, }); ``` and press `Hide entire text`
m-bert
reviewed
Nov 13, 2025
m-bert
reviewed
Nov 14, 2025
m-bert
left a comment
Collaborator
There was a problem hiding this comment.
LGTM, though I'll leave approval for @akwasniewski as he is the author of VirtualDetector 😅
akwasniewski
left a comment
Contributor
There was a problem hiding this comment.
Looks good, you managed to solve most of my workarounds, congrats.
j-piasecki
marked this pull request as draft
November 17, 2025 15:03
Member
Author
|
Found an infinite render loop 😞 |
Member
Author
|
The issue was switching between the Animated and non-animated intercepting detectors, which caused the tree to unmount, switch back to the other detector, and remount, triggering the whole process again. Should be gone with the new approach using |
j-piasecki
marked this pull request as ready for review
November 18, 2025 10:35
akwasniewski
approved these changes
Nov 18, 2025
m-bert
reviewed
Nov 18, 2025
akwasniewski
approved these changes
Nov 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The communication logic between
VirtualDetector/InterceptingGestureDetectorwas complex, since it was passing callbacks through ref and everything else through state via context.Since we're relying on Reanimated's
useEventanduseComposedEventHandler, we need to trigger a rerender anyway to update the handler. This means there's no point in trying to optimize passing callbacks via refs, as we need to end with a render, and synchronizing renders and state updates requires additional logic.This PR simplifies the communication layer to pass everything through the state, which should greatly simplify logic.
It also:
shouldUseReanimated,dispatchesAnimatedEventsflags - now it checks every virtual gesture, where previously the last registered one was the deciding factorVirtualDetectorso that children changes can be detected - this should be changed to use MutationObserver once it's rolled out in RNInterceptingGestureDetectorTest plan
Tested on the following snippet: