最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native TouchableOpacity not working on children - Stack Overflow

programmeradmin4浏览0评论

I have a TouchableOpacity that does not execute the onPress event when I click on a child ponent inside of it. The onpress event is a simple console.log();

<TouchableOpacity style={{flex: 1}} onPress={this.onPress.bind(this)}>
    {this.state.customComponent}
</TouchableOpacity>

If I click on an empty space inside of TouchableOpacity it will execute the onPress. If I click directly on the child ponent it will not execute.

Edit: I figured out the problem, it was because the TouchableOpacity had its child ponent render a TouchableHighlight unfortunately you you can't have a TouchableHighlight as a child

I have a TouchableOpacity that does not execute the onPress event when I click on a child ponent inside of it. The onpress event is a simple console.log();

<TouchableOpacity style={{flex: 1}} onPress={this.onPress.bind(this)}>
    {this.state.customComponent}
</TouchableOpacity>

If I click on an empty space inside of TouchableOpacity it will execute the onPress. If I click directly on the child ponent it will not execute.

Edit: I figured out the problem, it was because the TouchableOpacity had its child ponent render a TouchableHighlight unfortunately you you can't have a TouchableHighlight as a child

Share Improve this question edited Feb 18, 2016 at 0:34 Train asked Feb 17, 2016 at 23:54 TrainTrain 3,5263 gold badges31 silver badges65 bronze badges 2
  • 1 I remend setting up an example at rnplay so that we can witness for ourselves and tweak the code in helpful ways. – Chris Geirman Commented Feb 18, 2016 at 0:16
  • I'm experiencing this same exact problem, I have other touchable opacities within my app and they work perfectly well except one. – Steven Muganwa Commented Oct 22, 2019 at 5:49
Add a ment  | 

2 Answers 2

Reset to default 5

I had this problem today. The solution was an easy fix: I imported the TouchableOpacity from "react-native-gesture-handler", it should be imported from "react-native".

I know this is a few years late but I hope it helps someone. I had a similar issue and found that one of the parent views(Not immediate parent) was actually covering a large part of the Touchable opacity, I solved my issue by increasing the height of the above mentioned parent. It was difficult to find because its overflow was not hidden. Changing the parent elements overflow to hidden might help with finding out what's actually blocking the touchable opacity.

发布评论

评论列表(0)

  1. 暂无评论