TouchableOpacity swallow touch event and never pass

ⅰ亾dé卋堺 提交于 2021-01-27 22:01:05

问题


I'm trying to make both onPress to happened TouchableOpacity, but the second one is the only one that fires:

<TouchableOpacity onPress={() => console.warn('first button')}>
  <TouchableOpacity onPress={() => console.warn('second button')}>
    <Text>
      PRESS ME
    </Text>
  </TouchableOpacity>
</TouchableOpacity>

How can I make both of them fire?

Thanks in advance!


回答1:


You can fire both of them simply using the 'ref' concept and calling the props onPress.

<TouchableOpacity onPress={() => 
  AlertIOS.alert('firstbutton')
  } 
  ref={component => this.myFirstTouchable = component}
>
    <TouchableOpacity onPress={() => 
        {
        AlertIOS.alert('second')
        this.myFirstTouchable.props.onPress()
        }
    }>
        <Text>
          PRESS ME
        </Text>
      </TouchableOpacity>
    </TouchableOpacity>


来源:https://stackoverflow.com/questions/40572499/touchableopacity-swallow-touch-event-and-never-pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!