React Native: View onPress does not work
You can wrap the view with a TouchableWithoutFeedback
and then use onPress
and friends like usual. Also you can still block pointerEvents
by setting the attribute on on the child view, it even blocks pointer events on the parent TouchableWithoutFeedback
, its interesting, this was my need on Android, I didn't test on iOS:
https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html
<TouchableWithoutFeedback onPressIn={this.closeDrawer}>
<Animated.View style={[styles.drawerBackground, styleBackground]} pointerEvents={isOpen ? undefined : 'none'} />
</TouchableWithoutFeedback>
You can use TouchableOpacity
for onPress
event.
View
doesn't provide onPress
prop.
<TouchableOpacity style={{backgroundColor: "red", padding: 20}} onPress={()=> {
console.log('does not work');
}
}>
<Text>X</Text>
</TouchableOpacity>
Alternatively you can also provide onStartShouldSetResponder to your view, like so:
<View onStartShouldSetResponder={() => console.log("View click")}>
// some code here
</View>