I want to catch the event when a user taps on the view. Currently, I use the TouchableXXX
components to catch user onPress
(seems like onPress
catches the onTapDown
event only).
You will need to wrap your view object with TouchableOpacity or one of the other Touchable components. With TouchableOpacity you have the onPress prop that is passed a press event. From this press event you have access to the x,y coordinates of the press.
As Andrew said: You can wrap your View with TouchableWithoutFeedback and adding a onPress you can detect when the view is tapped.
Tapping any button will fire the respective onPress callback and dismiss the alert. By default, the only button will be an 'OK' button. This is an API that works both on Android and iOS and can show static alerts.
You can set onTouchStart
and onTouchEnd
handlers on any View
via props, for example:
<View onTouchStart={() => this.doSomething()} />
For general information about RN touches see the Handling Touches guide. In case you need a more complex, or custom touch handling, see the Gesture Responder System guide.
The accepted answer does not work anymore. The correct way is to use Touchable's onPressIn()
https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html#onpressin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With