Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger another Touchable component's press event by tap a touchable component?

How to trigger another Touchable component's press event by tap a touchable component? thanks!

Example:-

<TouchableHighlight onPress={this._triggerSecond}>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>

If user will tap on first TouchableHighlight then it should trigger second TouchableHighlight.

<TouchableHighlight>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>
like image 980
Irfan Ali Avatar asked Sep 12 '25 19:09

Irfan Ali


2 Answers

For those who still need to fire a press programmatically, I found a solution. First you need to bind your touchable to this by using ref :

<TouchableOpacity
  onPress={() => alert("click")}
  ref={(touchable) => this._touchable = touchable}
>

then when you want to launch onPress without clicking it just call

this._touchable.touchableHandlePress()
like image 108
Nicolas Meienberger Avatar answered Sep 14 '25 08:09

Nicolas Meienberger


Well I guess instead of thinking in that way I guess you can thinking this as just another TouchableHighlight calling the same method such as:

// First one 
<TouchableHighlight onPress={this._method}>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>

// Second one
<TouchableHighlight onPress={this._method}>
  <Image
    style={styles.button}
    source={require('./myButton.png')}
  />
</TouchableHighlight>

In this way both components when tap are going to invoke to the same method.

like image 40
Crisoforo Gaspar Avatar answered Sep 14 '25 09:09

Crisoforo Gaspar