How to trigger another Touchable component's p

2020-07-25 04:04发布

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>

2条回答
▲ chillily
2楼-- · 2020-07-25 04:43

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()
查看更多
Explosion°爆炸
3楼-- · 2020-07-25 04:57

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.

查看更多
登录 后发表回答