I'm facing a weird problem. In my react native app, if I set onPress
event to View
it is not triggered but if I set the same to Text
inside View
, it fires. What am I missing here?
<View style={{backgroundColor: "red", padding: 20}}>
<Text onPress={()=> {
console.log('works');
}
}>X</Text>
</View>
<View style={{backgroundColor: "red", padding: 20}} onPress={()=> {
console.log('does not work');
}
}>
<Text>X</Text>
</View>
Why is this so? Is this an issue with React Native? I'm using version 0.43
You can use
TouchableOpacity
foronPress
event.View
doesn't provideonPress
prop.Alternatively you can also provide onStartShouldSetResponder to your view, like so:
You can wrap the view with a
TouchableWithoutFeedback
and then useonPress
and friends like usual. Also you can still blockpointerEvents
by setting the attribute on on the child view, it even blocks pointer events on the parentTouchableWithoutFeedback
, its interesting, this was my need on Android, I didn't test on iOS:https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html
You can use TouchableOpacity, TouchableHighlight, TouchableNativeFeedback, to achieve this. View component doesn't provide onPress as props. So you use these instead of that.