admin管理员组

文章数量:1301514

How to trigger another Touchable ponent's press event by tap a touchable ponent? 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>

How to trigger another Touchable ponent's press event by tap a touchable ponent? 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>
Share asked Apr 12, 2017 at 12:15 Irfan AliIrfan Ali 2,2581 gold badge24 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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()

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 ponents when tap are going to invoke to the same method.

本文标签: