question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Touch events don't bubble up when nesting TouchableHighlight components

See original GitHub issue

I would expect unhandled touch events to bubble up to the parent when a given TouchableHighlight component doesn’t handle them. All the events currently stop at the direct container.

<TouchableHighlight onLongPress={this._onLongPress}>
     <TouchableHighlight onPress={this._onPress}>
           <Text>
               Press or LongPress me
           </Text>
     </TouchableHighlight>
</TouchableHighlight>

Is there a particular reason why we don’t allow the events to bubble up and enable the above scenario?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
brentvatnecommented, Apr 8, 2016

Somewhat related, check out this great talk by Andy Matuschak about the challenges of implementing a gesture system.

So this is what I think is going on: the touchable state is local to the touchable component, and touchable components are completely unaware of others and eager to become responder for any touch event that is given to it. Currently, there can only be one responder for any touch event. So when you touch the inner TouchableHighlight, it captures the touch which prevents the outer component from even having a chance to transition from the “not responder” state of its state machine. When you release the touch, it simply ends the gesture.

I wasn’t sure whether there was a good reason not to default to this, which seemed like the natural behavior to me.

My guess would be that it is a simple model that works most of the time. Maybe @vjeux can speak more to this.

If one were to write his own TouchableHighlight, he could override onStartShouldSetResponderCapture or onStartShouldSetResponder but would he be able to tell which events is coming (e.g. long press) in order to make the determination of whether there’s a handler for it and therefore whether to return true or false? I couldn’t find a way to do this.

I’m actually not sure that this approach would work and don’t have time at the moment to look into it 😦 Let me know if you find a solution to it.

0reactions
lackercommented, Oct 28, 2016

This issue seems like it has been sufficiently answered, so I am going to close it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I propagate touch event in nested Touchable in ...
I am not sure I understand the question but try using event.preventDefault on the outer touchable highlight, to only access the inner one....
Read more >
React Nested Component Touch Event Stop Propagation Is Not ...
The stopPropagation method prevents propagation of the same event from being called. Propagation means bubbling up to parent elements or capturing down to....
Read more >
Event Bubbling and Event Catching in JavaScript and React
In this article, I'll help you understand event bubbling and event catching like a ... Which Events Do Not Bubble and How Are...
Read more >
React Native touchable vs. pressable components
Learn the difference between touchable and pressable components in React ... touch events via buttons and other interactive UI elements.
Read more >
press event passes through overlaying <View/> on andriod ...
How to Bubbling press event from inner Touchable to outer Touchable in React-Native? ... How to trigger another Touchable component's press event by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found