Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop touch event propagation in React-Native

Tags:

I have a scrollview with a grid of images when I long press on an image I’d like to stop propagating the mouse events to the scrollview and just monitor the movements. With the intent to re-initialize propagation on press out. Anyone know how?

like image 708
aintnorest Avatar asked Aug 06 '15 22:08

aintnorest


People also ask

How do you stop event propagation in React Native?

Just add the prop onStartShouldSetResponder={event => true} to the <View> you want to prevent bubbling up.

How do you stop event propagation?

To stop an event from further propagation in the capturing and bubbling phases, you call the Event. stopPropation() method in the event handler. Note that the event. stopPropagation() method doesn't stop any default behaviors of the element e.g., link click, checkbox checked.

How do I disable TouchableOpacity?

React Native touchableopacity provide a disabled attribute to disable touchableopacity, you have to just pass true in the disabled attribute like the below example.

What is onPress in React Native?

Pressing the button will call the "onPress" function, which in this case displays an alert popup. If you like, you can specify a "color" prop to change the color of your button.


2 Answers

This might be new since the previous answers, but I find you can just gobble the event using another "touchable":

<TouchableOpacity onPress={this.onPressOuter}>     <TouchableOpacity activeOpacity={1}>         <Text>Content</Text>     </TouchableOpacity> </TouchableOpacity> 

In this example, touching the text does not trigger onPressOuter

like image 53
pstanton Avatar answered Sep 18 '22 07:09

pstanton


I solved this issue by wrap my press event with class method that set inner variable to true, then did the original logic, and after it finished, set again the inner variable to false. Then, you can wrap your container component event handler to check if inner variable set to true or false. for example:

<TouchableOpacity onPress={this.doSomething1}>     <TouchableOpacity onPress={this.doSomething2}>         <Image ... />     </TouchableOpacity> </TouchableOpacity>  doSomething1() {     this.preventDefault = true;     doSomeLogic();     this.preventDefault = false; }  doSomething2() {     if(!this.preventDefault) {      } } 
like image 42
Yossi Avatar answered Sep 17 '22 07:09

Yossi